codea: equal
[uebersetzerbau-ss10.git] / codea / chelper.c
index bb0f7005ea1ccd5ac72f56087485889a8e04c9a2..bbbb3617fbd2756b10d53985cf3d7830aad8b6f6 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include "chelper.h"
 #include "tree.h"
 
@@ -12,6 +13,11 @@ 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(strcmp(src,dst) != 0) {
@@ -19,17 +25,27 @@ void move(char *src, char *dst)
        }
 }
 
+void moveimm(long imm, char *dst)
+{
+       char buf[100];
+       sprintf(buf, "$%li", 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)
 {
+       /* TODO: bessere registerwahl. das is gerade a wengal suboptimal... */
+#define REGLEN 9
        char *regs[] = {"rax", "r10", "r11", "r9", "r8", "rcx", "rdx", "rsi", "rdi"};
+
        int i=0;
        if (s != (char*) NULL) {
-               while(i < 9) {
+               while(i < REGLEN) {
                        if(!strcmp(s, regs[i++])) {
                                break;
                        }
@@ -39,8 +55,16 @@ 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", regs[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", regs[i], params, i);
+               /* TODO: exit hier? */
+#if 0
+               exit(4);
+#endif
+       }
 
        return regs[i];
 }