codea: chelper aka 'code helper'
authorBernhard Urban <lewurm@gmail.com>
Thu, 22 Apr 2010 14:21:20 +0000 (16:21 +0200)
committerBernhard Urban <lewurm@gmail.com>
Thu, 22 Apr 2010 14:21:20 +0000 (16:21 +0200)
... hilfsfunktionen zur registerwahl und aufbaufkts fuer assemblerfile

codea/Makefile
codea/chelper.c [new file with mode: 0644]
codea/chelper.h [new file with mode: 0644]

index 1553b6b589eb3fa244c80ea5d4db3efab1434d52..2b0df7d7676eb8f4c00293a09bbc293326a7b9e9 100644 (file)
@@ -1,7 +1,7 @@
 SHELL := bash
 NAME := codea
 CFLAGS := -ansi -pedantic -D_GNU_SOURCE
-OBJS := scanner.o parser.o symtable.o code.o
+OBJS := scanner.o parser.o symtable.o code.o chelper.o
 
 all: $(NAME)
 
@@ -13,11 +13,12 @@ scanner.c: oxout.l
        @echo "  FLEX    $<"
        @flex -o$@ $<
 
-%.o: %.c parser.h symtable.h 
+#dirty deps ;)
+%.o: %.c parser.h symtable.h chelper.h
        @echo "  CC      $<"
        @gcc -c $(CFLAGS) $< #-Wall
 
-parser.c: oxout.y
+parser.c: oxout.y chelper.h
        @echo "  YACC    $<"
        @yacc -t -v -d $< -o $@
 
@@ -27,7 +28,7 @@ oxout.y oxout.l: parser.y scanner.lex
        @echo "  OX      $^"
        @ox $^
 
-%.c: %.bfe
+%.c: %.bfe chelper.h
        @echo "  IBURG   $<"
        @bfe < $< | iburg > $@
 
diff --git a/codea/chelper.c b/codea/chelper.c
new file mode 100644 (file)
index 0000000..c590232
--- /dev/null
@@ -0,0 +1,44 @@
+#include <stdio.h>
+#include <string.h>
+#include "chelper.h"
+
+void func_header(char *s)
+{
+       printf("\t.globl %1$s\n\t.type %1$s, @function\n%1$s:\n", s);
+}
+
+void move(char *src, char *dst)
+{
+       if(strcmp(src,dst) != 0) {
+               printf("\tmovq %%%s, %%%s\n", src, dst);
+       }
+}
+
+void ret(void)
+{
+       printf("\tret\n");
+}
+
+char *next_reg(char *s, short skip)
+{
+       char *regs[] = {"rax", "r10", "r11", "r9", "r8", "rcx", "rdx", "rsi", "rdi"};
+       int i=0;
+       if (s != (char*) NULL) {
+               for(; i < 9;) {
+                       if(!strcmp(s, regs[i++])) {
+                               break;
+                       }
+               }
+       }
+       if(skip) {
+               i++;
+       }
+       return regs[i];
+}
+
+char *param_reg(int num)
+{
+       char *regs[] = {"rdi", "rsi", "rdx", "rcx", "r8", "r9"};
+       return regs[num-1];
+}
+
diff --git a/codea/chelper.h b/codea/chelper.h
new file mode 100644 (file)
index 0000000..9336fd6
--- /dev/null
@@ -0,0 +1,8 @@
+#ifndef __CHELPER_H
+#define __CHELPER_H
+void func_header(char *s);
+char *next_reg(char *s, short skip);
+char *param_reg(int num);
+void ret(void);
+void move(char *src, char *dest);
+#endif