arm64: codea/abgabe_aa.0
[uebersetzerbau-ss10.git] / gesamt_arm64 / 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 /* TODO */
12 #define REGLEN 5
13 static char *regsppc[] = {"x10", "x11", "x12", "x13", "x14"};
14
15 /* ja, dirty.. */
16 static char *akt_func_name = (char*) NULL;
17 static char need_stack = 0;
18
19 void func_header(char *s, int vars, int parms, int call)
20 {
21         printf("\t.global %1$s\n\t.type %1$s, %%function\n%1$s:\n", s);
22         printf("\t// vars: %i, parms: %i, call(bool): %i\n", vars, parms, call);
23         akt_func_name = s;
24
25         need_stack = (vars || parms) && call;
26         if(need_stack) {
27                 /* save the link register */
28                 printf("\tmflr 0; stw 0,-8(1)\n");
29                 /* create the stack */
30                 printf("\tstwu 1, -80(1)\n");
31         }
32 }
33
34 char *get_func_name(void)
35 {
36         return akt_func_name;
37 }
38
39 void func_footer(void)
40 {
41         if(need_stack) {
42                 /* remove stack frame */
43                 printf("\taddi 1,1,80\n");
44                 /* restore link register */
45                 printf("\tlwz 0,-8(1); mtlr 0\n");
46         }
47         printf("\tret\n\n\n");
48 }
49
50 void move(char *src, char *dst)
51 {
52         if(src == (char*) NULL) return;
53         if(strcmp(src,dst) != 0) {
54                 printf("\tmov %s, %s\n", dst, src);
55         }
56 }
57
58 void moveimm(long imm, char *dst)
59 {
60         static int constlbl = 1;
61         if((imm > 65536-1) || (imm < -65536)) {
62                 printf("\t.align 2\n\t.CONSTLBL%i\n\t.word %i\n", constlbl++);
63                 printf("\tldr %s, .CONSTLBL%i\n", dst, constlbl);
64         } else {
65                 /* just low word */
66                 printf("\t%s %s, #%d\n", imm >= 0 ? "mov" : "mvn", dst, imm >= 0 ? imm : (-1 * imm) - 1);
67         }
68 }
69
70 char *next_reg(char *s, int params)
71 {
72         int i = 0;
73         if (s != (char*) NULL) {
74                 while(strcmp(s, regsppc[i]) != 0) {
75                         i = (i+1) % REGLEN;
76                 }
77                 i = (i+1) % REGLEN;
78         }
79 #ifdef DDCHELP
80         fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regsppc[i], params);
81 #endif
82         return regsppc[i];
83 }
84
85 char *reg_64to8l(char *s)
86 {
87         fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
88         exit(4);
89         return "";
90 }
91
92 char *param_reg(int num)
93 {       /* TODO */
94         char *regs[] = {"x0", "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9"};
95         return regs[num];
96 }
97