ppc: codea_abgabe_a{i,h} gehen jetzt auch, bin mir dabei aber ned ganz sicher...
[uebersetzerbau-ss10.git] / codea / chelper.c
index 9f5032cd500af79944678cc986a063f740271458..015e7a35244bfd13de60e33cd2f4584737f642f9 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include "chelper.h"
 #include "tree.h"
 
@@ -7,6 +8,10 @@
 #define DDCHELP
 #endif
 
+#define REGLEN 4
+static char *regs64[] = {"rax", "r10", "r11", "r9"};
+static char *regs8l[] = {"al", "r10b", "r11b", "r9b"};
+
 void func_header(char *s)
 {
        printf("\t.globl %1$s\n\t.type %1$s, @function\n%1$s:\n", s);
@@ -27,39 +32,36 @@ void move(char *src, char *dst)
 void moveimm(long imm, char *dst)
 {
        char buf[100];
-       sprintf(buf, "$%li", imm);
+       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, int params)
 {
-       /* TODO: bessere registerwahl. das is gerade a wengal suboptimal... */
-#if 1
-       char *regs[] = {"rax", "r10", "r11", "rax"};
-#else
-       char *regs[] = {"rax", "r10", "r11", "r9", "r8", "rcx", "rdx", "rsi", "rdi"};
-#endif
-       int i=0;
+       int i = 0;
        if (s != (char*) NULL) {
-               while(i < 9) {
-                       if(!strcmp(s, regs[i++])) {
-                               break;
-                       }
+               while(strcmp(s, regs64[i]) != 0) {
+                       i = (i+1) % REGLEN;
                }
-       }
-       if(skip) {
-               i++;
+               i = (i+1) % REGLEN;
        }
 #ifdef DDCHELP
-       fprintf(stderr, "next_reg(): %s\n", regs[i]);
+       fprintf(stderr, "next_reg(): %s (bei %i parameter)\n", regs64[i], params);
 #endif
+       return regs64[i];
+}
 
-       return regs[i];
+char *reg_64to8l(char *s)
+{
+       int i = 0;
+       if (s != (char*) NULL) {
+               while(strcmp(s, regs64[i]) != 0) {
+                       i = (i+1) % REGLEN;
+               }
+               return regs8l[i];
+       }
+       fprintf(stderr, "reg_64to8l(): sollte nicht passieren\n");
+       exit(4);
 }
 
 char *param_reg(int num)