From 6ed5d059934e387ba16b27cc26d91ba20e04b632 Mon Sep 17 00:00:00 2001 From: Morten Delenk Date: Sat, 2 Jul 2016 14:11:08 +0200 Subject: [PATCH] Initial commit --- Makefile | 27 +++++++++++++ crt0.asm | 4 ++ libmtg.asm | 10 +++++ mumei.l | 23 +++++++++++ mumei.y | 110 +++++++++++++++++++++++++++++++++++++++++++++++++++++ test.mtl | 6 +++ 6 files changed, 180 insertions(+) create mode 100644 Makefile create mode 100644 crt0.asm create mode 100644 libmtg.asm create mode 100644 mumei.l create mode 100644 mumei.y create mode 100644 test.mtl diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6eff2e1 --- /dev/null +++ b/Makefile @@ -0,0 +1,27 @@ +CC=gcc +CXX=g++ +LD=g++ +CFLAGS=-O3 -march=native -fstack-protector=strong +CXXFLAGS=-O3 -march=native-fstack-protector=strong +LEX=flex +YACC=bison +all: mumei + +mumei: mumei.tab.o lex.yy.o + $(LD) -o $@ $^ -lfl + +lex.yy.o: lex.yy.c + $(CXX) $(CPPFLAGS) -c -o $@ $^ + +mumei.tab.o: mumei.tab.c + $(CXX) $(CPPFLAGS) -c -o $@ $^ + +lex.yy.c: mumei.tab.c + $(LEX) mumei.l + +mumei.tab.c: + $(YACC) -d mumei.y + +.PHONY: clean all +clean: + rm -rf mumei.tab.c mumei.tab.h lex.yy.c mumei *.o \ No newline at end of file diff --git a/crt0.asm b/crt0.asm new file mode 100644 index 0000000..7c170cc --- /dev/null +++ b/crt0.asm @@ -0,0 +1,4 @@ +_start: + cpget + jmp @main + [i0:zero] syscall [ci:0] \ No newline at end of file diff --git a/libmtg.asm b/libmtg.asm new file mode 100644 index 0000000..c358514 --- /dev/null +++ b/libmtg.asm @@ -0,0 +1,10 @@ +puti: + bpget + spget + bpset + get -2 + [i0:pop] syscall [ci:2] + bpget + spset + bpset + ret \ No newline at end of file diff --git a/mumei.l b/mumei.l new file mode 100644 index 0000000..2d369dc --- /dev/null +++ b/mumei.l @@ -0,0 +1,23 @@ +%{ +#include +#include +using namespace std; +#define YY_DECL extern "C" int yylex() +#include "mumei.tab.h" +int linenum=0; +%} +%% +[ \t\n] ; +FUNCTION { return FUNCTION;} +END { return END;} +[0-9]+ { yylval.ival = atoi(yytext); return INT;} +[a-zA-Z][a-zA-Z0-9]* {yylval.sval=strdup(yytext);return VARNAME;} +\+ { return PLUS;} +\- { return MINUS;} +\( { return LPARENS; } +\) { return RPARENS; } +\* { return MUL;} +\/ { return DIV;} +\= { return EQ;} +\, { return COMMA;} +%% \ No newline at end of file diff --git a/mumei.y b/mumei.y new file mode 100644 index 0000000..74b7281 --- /dev/null +++ b/mumei.y @@ -0,0 +1,110 @@ +%{ +#include +#include +#include +#include +#include +#include +#include +#include +#include +using namespace std; +extern "C" int yylex(); +extern "C" int yyparse(); +extern "C" FILE *yyin; +void yyerror(const char *s); +vector nameTable; +int getVarID(string name) { + auto it=nameTable.end(); + for(auto it=nameTable.begin(); it!=nameTable.end(); it++) { + if(nameTable[it-nameTable.begin()]==name) + return it-nameTable.begin(); + } + nameTable.push_back(name); + return nameTable.size()-1; +} +ofstream out; +%} +%union { + int ival; + char* sval; +} +%token INT +%token PLUS +%token MINUS +%token MUL +%token LPARENS +%token RPARENS +%token DIV +%token VARNAME +%token EQ +%token COMMA +%token FUNCTION +%token END +%left PLUS MINUS MUL DIV FUNCTION +%right EQ +%% +program: + fh function ef program + | fh function ef +fh: + FUNCTION VARNAME {out << $2 << ":\n\tbpget\n\tspget\n\tbpset" << endl;} +ef: + END {out << "\tbpget\n\tspset\n\tbpset\n\tret"<