af9cf4cebf88ba3a82a33ff3f841e9cfd65279e0
[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(strcmp(src,dst) != 0) {
28                 printf("\tmovq %%%s, %%%s\n", src, dst);
29         }
30 }
31
32 void moveimm(long imm, char *dst)
33 {
34         char buf[100];
35         sprintf(buf, "$%d", imm);
36         printf("\tmovq %s, %%%s\n", buf, dst);
37 }
38
39 void ret(void)
40 {
41         printf("\tret\n");
42 }
43
44 char *next_reg(char *s, short skip, int params)
45 {
46         int i = 0;
47         if (s != (char*) NULL) {
48                 while(i < REGLEN) {
49                         if(!strcmp(s, regs64[i++])) {
50                                 break;
51                         }
52                 }
53         }
54         if(skip) {
55                 i++;
56         }
57 #ifdef DDCHELP
58         fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regs64[i], params);
59 #endif
60         /* TODO: <= passt? */
61         if(REGLEN - params <= i) {
62                 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);
63                 /* TODO: exit hier? */
64 #if 0
65                 exit(4);
66 #endif
67         }
68         return regs64[i];
69 }
70
71 char *reg_64to8l(char *s)
72 {
73         int i = 0;
74         if (s != (char*) NULL) {
75                 while(i < REGLEN) {
76                         if(!strcmp(s, regs64[i])) {
77                                 return regs8l[i];
78                         } else {
79                                 i++;
80                         }
81                 }
82         }
83         fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
84         exit(4);
85 }
86
87
88 char *param_reg(int num)
89 {
90         char *regs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
91         return regs[num];
92 }
93