codeb: if mit lblcnt (=labelcounter)
[uebersetzerbau-ss10.git] / codeb / chelper.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include "chelper.h"
5 #include "tree.h"
6
7 #if 0
8 #define DDCHELP
9 #endif
10
11 #define REGLEN 4
12 static char *regs64[] = {"rax", "r10", "r11", "r9"};
13 static char *regs8l[] = {"al", "r10b", "r11b", "r9b"};
14
15 /* ja, dirty.. */
16 static char *akt_func_name = (char*) NULL;
17 void func_header(char *s)
18 {
19         printf("\t.globl %1$s\n\t.type %1$s, @function\n%1$s:\n", s);
20         akt_func_name = s;
21 }
22
23 char *get_func_name(void)
24 {
25         return akt_func_name;
26 }
27
28 void func_footer(void)
29 {
30         printf("\tret\n");
31 }
32
33 void move(char *src, char *dst)
34 {
35         if(strcmp(src,dst) != 0) {
36                 printf("\tmovq %%%s, %%%s\n", src, dst);
37         }
38 }
39
40 void moveimm(long imm, char *dst)
41 {
42         char buf[100];
43         sprintf(buf, "$%d", imm);
44         printf("\tmovq %s, %%%s\n", buf, dst);
45 }
46
47 char *next_reg(char *s, int params)
48 {
49         int i = 0;
50         if (s != (char*) NULL) {
51                 while(strcmp(s, regs64[i]) != 0) {
52                         i = (i+1) % REGLEN;
53                 }
54                 i = (i+1) % REGLEN;
55         }
56 #ifdef DDCHELP
57         fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regs64[i], params);
58 #endif
59         return regs64[i];
60 }
61
62 char *reg_64to8l(char *s)
63 {
64         int i = 0;
65         if (s != (char*) NULL) {
66                 while(strcmp(s, regs64[i]) != 0) {
67                         i = (i+1) % REGLEN;
68                 }
69                 return regs8l[i];
70         }
71         fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
72         exit(4);
73 }
74
75 char *param_reg(int num)
76 {
77         char *regs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
78         return regs[num];
79 }
80