codea: w00t... snafu_03 geht _ENDLICH_ *pardy* :)))
[uebersetzerbau-ss10.git] / codea / chelper.c
index bb0f7005ea1ccd5ac72f56087485889a8e04c9a2..0be9cc3c06a2d9f2766f31f6a7c73e0aee4f7871 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include "chelper.h"
 #include "tree.h"
 
@@ -7,30 +8,56 @@
 #define DDCHELP
 #endif
 
+#define REGLEN 10
+#if 0
+static char *regs64[] = {"rax", "r10", "r11", "r9", "r8", "rcx", "rdx", "rsi", "rdi"};
+static char *regs8l[] = {"al", "r10b", "r11b", "r9b", "r8b", "cl", "dl", "sil", "dil"};
+#else
+static char *regs64[] = {"rax", "r10", "r11", "r9", "rax", "r10", "r11", "rax", "r10", "r11"};
+static char *regs8l[] = {"al", "r10b", "r11b", "r9b", "al", "r10b", "r11b", "al", "r10b", "r11b"};
+#endif
+
 void func_header(char *s)
 {
        printf("\t.globl %1$s\n\t.type %1$s, @function\n%1$s:\n", s);
 }
 
+void func_footer(void)
+{
+       printf("\tret\n");
+}
+
 void move(char *src, char *dst)
 {
+       if(src == NULL) {
+               printf("//wtf, src ist null\n");
+       }
+       if(dst == NULL) {
+               printf("//wtf, dst ist null\n");
+       }
        if(strcmp(src,dst) != 0) {
                printf("\tmovq %%%s, %%%s\n", src, dst);
        }
 }
 
+void moveimm(long imm, char *dst)
+{
+       char buf[100];
+       sprintf(buf, "$%d", imm);
+       printf("\tmovq %s, %%%s\n", buf, dst);
+}
+
 void ret(void)
 {
        printf("\tret\n");
 }
 
-char *next_reg(char *s, short skip)
+char *next_reg(char *s, short skip, int params)
 {
-       char *regs[] = {"rax", "r10", "r11", "r9", "r8", "rcx", "rdx", "rsi", "rdi"};
-       int i=0;
+       int i = 0;
        if (s != (char*) NULL) {
-               while(i < 9) {
-                       if(!strcmp(s, regs[i++])) {
+               while(i < REGLEN) {
+                       if(!strcmp(s, regs64[i++])) {
                                break;
                        }
                }
@@ -39,12 +66,36 @@ char *next_reg(char *s, short skip)
                i++;
        }
 #ifdef DDCHELP
-       fprintf(stderr, "next_reg(): %s\n", regs[i]);
+       fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regs64[i], params);
+#endif
+       /* TODO: <= passt? */
+       if(REGLEN - params <= i) {
+               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);
+               /* TODO: exit hier? */
+#if 0
+               exit(4);
 #endif
+       }
+       return regs64[i];
+}
 
-       return regs[i];
+char *reg_64to8l(char *s)
+{
+       int i = 0;
+       if (s != (char*) NULL) {
+               while(i < REGLEN) {
+                       if(!strcmp(s, regs64[i])) {
+                               return regs8l[i];
+                       } else {
+                               i++;
+                       }
+               }
+       }
+       fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
+       exit(4);
 }
 
+
 char *param_reg(int num)
 {
        char *regs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};