25893f9ae67873ba743bb8b5e4585a09d05fddaf
[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 4
12 static char *regs64[] = {"rax", "r10", "r11", "r9"};
13 static char *regs8l[] = {"al", "r10b", "r11b", "r9b"};
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(strcmp(s, regs64[i]) != 0) {
55                         i = (i+1) % REGLEN;
56                 }
57                 i = (i+1) % REGLEN;
58         }
59         if(skip) {
60                 i = (i+1) % REGLEN;
61         }
62 #ifdef DDCHELP
63         fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regs64[i], params);
64 #endif
65         /* TODO: <= passt? */
66         if(REGLEN - params <= i) {
67                 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);
68                 /* TODO: exit hier? */
69 #if 0
70                 exit(4);
71 #endif
72         }
73         return regs64[i];
74 }
75
76 char *reg_64to8l(char *s)
77 {
78         int i = 0;
79         if (s != (char*) NULL) {
80                 while(strcmp(s, regs64[i]) != 0) {
81                         i = (i+1) % REGLEN;
82                 }
83                 return regs8l[i];
84         }
85         fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
86         exit(4);
87 }
88
89
90 char *param_reg(int num)
91 {
92         char *regs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
93         return regs[num];
94 }
95