codeb: init von codea
[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 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 char *next_reg(char *s, int params)
40 {
41         int i = 0;
42         if (s != (char*) NULL) {
43                 while(strcmp(s, regs64[i]) != 0) {
44                         i = (i+1) % REGLEN;
45                 }
46                 i = (i+1) % REGLEN;
47         }
48 #ifdef DDCHELP
49         fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regs64[i], params);
50 #endif
51         return regs64[i];
52 }
53
54 char *reg_64to8l(char *s)
55 {
56         int i = 0;
57         if (s != (char*) NULL) {
58                 while(strcmp(s, regs64[i]) != 0) {
59                         i = (i+1) % REGLEN;
60                 }
61                 return regs8l[i];
62         }
63         fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
64         exit(4);
65 }
66
67 char *param_reg(int num)
68 {
69         char *regs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
70         return regs[num];
71 }
72