codea: w00t... snafu_03 geht _ENDLICH_ *pardy* :)))
[uebersetzerbau-ss10.git] / codea / 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 1
8 #define DDCHELP
9 #endif
10
11 #define REGLEN 10
12 #if 0
13 static char *regs64[] = {"rax", "r10", "r11", "r9", "r8", "rcx", "rdx", "rsi", "rdi"};
14 static char *regs8l[] = {"al", "r10b", "r11b", "r9b", "r8b", "cl", "dl", "sil", "dil"};
15 #else
16 static char *regs64[] = {"rax", "r10", "r11", "r9", "rax", "r10", "r11", "rax", "r10", "r11"};
17 static char *regs8l[] = {"al", "r10b", "r11b", "r9b", "al", "r10b", "r11b", "al", "r10b", "r11b"};
18 #endif
19
20 void func_header(char *s)
21 {
22         printf("\t.globl %1$s\n\t.type %1$s, @function\n%1$s:\n", s);
23 }
24
25 void func_footer(void)
26 {
27         printf("\tret\n");
28 }
29
30 void move(char *src, char *dst)
31 {
32         if(src == NULL) {
33                 printf("//wtf, src ist null\n");
34         }
35         if(dst == NULL) {
36                 printf("//wtf, dst ist null\n");
37         }
38         if(strcmp(src,dst) != 0) {
39                 printf("\tmovq %%%s, %%%s\n", src, dst);
40         }
41 }
42
43 void moveimm(long imm, char *dst)
44 {
45         char buf[100];
46         sprintf(buf, "$%d", imm);
47         printf("\tmovq %s, %%%s\n", buf, dst);
48 }
49
50 void ret(void)
51 {
52         printf("\tret\n");
53 }
54
55 char *next_reg(char *s, short skip, int params)
56 {
57         int i = 0;
58         if (s != (char*) NULL) {
59                 while(i < REGLEN) {
60                         if(!strcmp(s, regs64[i++])) {
61                                 break;
62                         }
63                 }
64         }
65         if(skip) {
66                 i++;
67         }
68 #ifdef DDCHELP
69         fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regs64[i], params);
70 #endif
71         /* TODO: <= passt? */
72         if(REGLEN - params <= i) {
73                 fprintf(stderr, "next_reg(): register \"%s\" in dem sich ein parameter befindet wird als temporaeres register verwendet(params: %i, i: %i)\n", regs64[i], params, i);
74                 /* TODO: exit hier? */
75 #if 0
76                 exit(4);
77 #endif
78         }
79         return regs64[i];
80 }
81
82 char *reg_64to8l(char *s)
83 {
84         int i = 0;
85         if (s != (char*) NULL) {
86                 while(i < REGLEN) {
87                         if(!strcmp(s, regs64[i])) {
88                                 return regs8l[i];
89                         } else {
90                                 i++;
91                         }
92                 }
93         }
94         fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
95         exit(4);
96 }
97
98
99 char *param_reg(int num)
100 {
101         char *regs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
102         return regs[num];
103 }
104