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