From 2acc4ed29dd5c5d034c4cb1ebc40c785829a4fea Mon Sep 17 00:00:00 2001 From: David Given Date: Sat, 24 Sep 2016 18:31:35 +0200 Subject: [PATCH] IR codes are now owned by mcgg; ir terminals are inserted into the table during compilation (so you can refer to them). --- mach/proto/mcg/build.lua | 15 +----- mach/proto/mcg/ir.dat | 71 ------------------------ mach/proto/mcg/ircodes.sh | 36 ------------- mach/proto/mcg/mcgg_generated_footer.h | 16 +++--- mach/proto/mcg/table | 17 +++--- util/mcgg/build.lua | 25 ++++++++- util/mcgg/iburg.c | 62 +++++++++++++++++++-- util/mcgg/iburg.h | 11 ++++ util/mcgg/ir.dat | 74 ++++++++++++++++++++++++++ util/mcgg/ircodes.sh | 61 +++++++++++++++++++++ 10 files changed, 244 insertions(+), 144 deletions(-) delete mode 100644 mach/proto/mcg/ir.dat delete mode 100755 mach/proto/mcg/ircodes.sh create mode 100644 util/mcgg/ir.dat create mode 100755 util/mcgg/ircodes.sh diff --git a/mach/proto/mcg/build.lua b/mach/proto/mcg/build.lua index 246a11ee3..b9440a790 100644 --- a/mach/proto/mcg/build.lua +++ b/mach/proto/mcg/build.lua @@ -1,17 +1,5 @@ include("util/mcgg/build.lua") -normalrule { - name = "ircodes", - outleaves = { "ircodes.h", "ircodes.c" }, - ins = { - "./ircodes.sh", - "./ir.dat" - }, - commands = { - "%{ins[1]} %{ins[2]} %{outs[1]} %{outs[2]}" - } -} - mcgg { name = "mcgg_c", srcs = { "./table" } @@ -22,10 +10,9 @@ cprogram { srcs = { "./*.c", "+mcgg_c", - matching(filenamesof("+ircodes"), "%.c$") }, deps = { - "+ircodes", + "util/mcgg+lib", "h+emheaders", "modules+headers", "modules/src/alloc+lib", diff --git a/mach/proto/mcg/ir.dat b/mach/proto/mcg/ir.dat deleted file mode 100644 index 36787525c..000000000 --- a/mach/proto/mcg/ir.dat +++ /dev/null @@ -1,71 +0,0 @@ -# Simple terminals -CONST -REG -LABEL -BLOCK -PAIR -ANY -LOCAL -PHI - -# Magic stack operations -PUSH -POP -SET - -# Memory operations -LOAD -STORE - -# Arithemetic operations -ADD -SUB -MUL -DIV -MOD -NEG - -ADDF -SUBF -MULF -DIVF -NEGF - -AND -OR -EOR -NOT - -# Conversions -CII1 -CII2 -CII4 -CII8 - -CIU1 -CIU2 -CIU4 -CIU8 - -# Tristate comparisons -COMPARES -COMPAREU - -# Boolean comparisons -IFEQ -IFLT -IFLE - -# Procedures -CALL - -# Flow control --- these never return -JUMP -CJUMP -RET - -# Special -STACKADJUST -GETRET -SETRET - diff --git a/mach/proto/mcg/ircodes.sh b/mach/proto/mcg/ircodes.sh deleted file mode 100755 index 3fdf1982d..000000000 --- a/mach/proto/mcg/ircodes.sh +++ /dev/null @@ -1,36 +0,0 @@ -#!/bin/sh - -in=$1 -header=$2 -source=$3 - -awk -f - $in >$header << "EOF" - BEGIN { - print "enum ir_opcode {" - } - - /^[^#]+/ { - print "\tIR_" $1 "," - } - - END { - print "};" - } -EOF - -awk -f - $in >$source << "EOF" - BEGIN { - print "#include \"mcg.h\"" - print "#include \"ir.h\"" - print "const char* ir_names[] = {" - } - - /^[^#]+/ { - printf("\t\"%s\",\n", $1) - } - - END { - print "};" - } -EOF - diff --git a/mach/proto/mcg/mcgg_generated_footer.h b/mach/proto/mcg/mcgg_generated_footer.h index 5b4d20109..56bdc9168 100644 --- a/mach/proto/mcg/mcgg_generated_footer.h +++ b/mach/proto/mcg/mcgg_generated_footer.h @@ -31,17 +31,17 @@ static NODEPTR_TYPE tree(int op, NODEPTR_TYPE l, NODEPTR_TYPE r) { int main(void) { NODEPTR_TYPE p; - p = tree(STORE, - tree(ADD, - tree(LABEL, 0, 0), - tree(CONST, 0, 0) + p = tree(STORE4, + tree(ADD4, + tree(LABEL4, 0, 0), + tree(CONST4, 0, 0) ), - tree(ADD, - tree(LOAD, - tree(LABEL, 0, 0), + tree(ADD4, + tree(LOAD4, + tree(LABEL4, 0, 0), 0 ), - tree(CONST, 0, 0) + tree(CONST4, 0, 0) ) ); burm_label(p); diff --git a/mach/proto/mcg/table b/mach/proto/mcg/table index f9a51c904..dc0bc0b3d 100644 --- a/mach/proto/mcg/table +++ b/mach/proto/mcg/table @@ -29,20 +29,19 @@ static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) { } #endif %} -%term LOAD STORE LABEL CONST ADD FOO BAR BAZ BOO; %% - stm = STORE(addr:address, value:reg) + stm = STORE4(addr:address, value:reg) ins value:GPR emit "str %value, %addr" cost 4; - reg = LOAD(addr:address) + reg = LOAD4(addr:address) outs dest:ANY emit "ld %dest, %addr" cost 4; - address = ADD(addr:reg, offset:CONST) + address = ADD4(addr:reg, offset:CONST) ins addr:GPR fragment "[%addr, #%offset.ivalue]"; @@ -52,19 +51,19 @@ static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) { stm = reg; - reg = ADD(left:reg, right:aluparam) + reg = ADD4(left:reg, right:aluparam) ins left:GPR, right:GPR outs out:GPR emit "add %out, %left, %right" cost 4; - reg = ADD(left:aluparam, right:reg) + reg = ADD4(left:aluparam, right:reg) ins left:GPR, right:GPR outs out:GPR emit "add %out, %right, %left" cost 4; - aluparam = value:CONST + aluparam = value:CONST4 when { return false; } fragment "#%value.ivalue"; @@ -75,12 +74,12 @@ static void burm_trace(NODEPTR_TYPE p, int eruleno, int cost, int bestcost) { emit "mov %out, %value" cost 4; - reg = value:LABEL + reg = value:LABEL4 outs out:GPR emit "adr %out, #value.lvalue" cost 4; - reg = value:CONST + reg = value:CONST4 outs out:GPR emit "ldr %out, #value.lvalue" cost 4; diff --git a/util/mcgg/build.lua b/util/mcgg/build.lua index a7d3cfde3..8afdbc0a9 100644 --- a/util/mcgg/build.lua +++ b/util/mcgg/build.lua @@ -10,6 +10,28 @@ yacc { srcs = { "./*.y" }, } +normalrule { + name = "ircodes", + outleaves = { "ircodes.h", "ircodes.c" }, + ins = { + "./ircodes.sh", + "./ir.dat" + }, + commands = { + "%{ins[1]} %{ins[2]} %{outs[1]} %{outs[2]}" + } +} + +clibrary { + name = "lib", + srcs = { + matching(filenamesof("+ircodes"), "%.c$") + }, + hdrs = { + matching(filenamesof("+ircodes"), "%.h$") + } +} + cprogram { name = "mcgg", srcs = { @@ -19,6 +41,7 @@ cprogram { }, deps = { "./*.h", + "+lib", "+yacc" } } @@ -51,7 +74,7 @@ definerule("mcgg", cpptable }, commands = { - "%{ins[1]} < %{ins[2]} > %{outs}", + "%{ins[1]} -i %{ins[2]} -o %{outs}", } } end diff --git a/util/mcgg/iburg.c b/util/mcgg/iburg.c index 05dd6c4ed..b4ef1442e 100644 --- a/util/mcgg/iburg.c +++ b/util/mcgg/iburg.c @@ -6,7 +6,9 @@ #include #include #include +#include #include "iburg.h" +#include "ircodes.h" static char rcsid[] = "$Id$"; @@ -23,6 +25,7 @@ static int nrules; static void print(char* fmt, ...); static void ckreach(Nonterm p); +static void registerterminals(void); static void emitclosure(Nonterm nts); static void emitcost(Tree t, char* v); static void emitcostcalc(Rule r); @@ -52,9 +55,12 @@ int main(int argc, char* argv[]) yydebug = 1; #endif + infp = stdin; + outfp = stdout; + for (;;) { - int opt = getopt(argc, argv, "p:"); + int opt = getopt(argc, argv, "p:i:o:"); if (opt == -1) break; @@ -64,16 +70,32 @@ int main(int argc, char* argv[]) prefix = optarg; break; + case 'i': + infp = fopen(optarg, "r"); + if (!infp) + { + yyerror("cannot open input file: %s\n", strerror(errno)); + exit(1); + } + break; + + case 'o': + outfp = fopen(optarg, "w"); + if (!outfp) + { + yyerror("cannot open output file: %s\n", strerror(errno)); + exit(1); + } + break; + default: yyerror("usage: %s [-p prefix] < input > output\n", argv[0]); exit(1); } } - infp = stdin; - outfp = stdout; - emitheader(); + registerterminals(); yyin = infp; yyparse(); @@ -126,6 +148,32 @@ char* stringf(char* fmt, ...) return p; } +static void registerterminal(const char* name, int iropcode, int size) +{ + const char* s = (size == 0) ? name : stringf("%s%d", name, size); + int esn = ir_to_esn(iropcode, size); + + term(s, esn); +} + +static void registerterminals(void) +{ + int i; + + for (i=0; ikind = TERM; diff --git a/util/mcgg/iburg.h b/util/mcgg/iburg.h index 5c5334c79..59bbfd7d8 100644 --- a/util/mcgg/iburg.h +++ b/util/mcgg/iburg.h @@ -86,4 +86,15 @@ extern int yylineno; extern void printlineno(void); +/* Excruciating macro which packs ir opcodes and sizes into an int for iburg's benefit. + * + * Sizes are mapped as: 0=1, 1=1, 2=2, 4=3, 8=4. + */ +#define ir_to_esn(iropcode, size) \ + ((iropcode)*4 + \ + (((size) == 4) ? 2 : \ + ((size) == 8) ? 3 : \ + ((size) == 0) ? 0 : \ + (size-1))) + #endif diff --git a/util/mcgg/ir.dat b/util/mcgg/ir.dat new file mode 100644 index 000000000..95059a650 --- /dev/null +++ b/util/mcgg/ir.dat @@ -0,0 +1,74 @@ +# Flags: +# S: has size (use in CONST1, CONST2, CONST4, CONST8 forms) +# V: has no size (use in JUMP, CJUMP, RET forms) + +# Simple terminals +S CONST +S REG +S LABEL +S BLOCK +S PAIR +S ANY +S LOCAL +S PHI + +# Magic stack operations +S PUSH +S POP + +# Memory operations +S LOAD +S STORE + +# Arithemetic operations +S ADD +S SUB +S MUL +S DIV +S MOD +S NEG + +S ADDF +S SUBF +S MULF +S DIVF +S NEGF + +S AND +S OR +S EOR +S NOT + +# Conversions +S CII1 +S CII2 +S CII4 +S CII8 + +S CIU1 +S CIU2 +S CIU4 +S CIU8 + +# Tristate comparisons +S COMPARES +S COMPAREU + +# Boolean comparisons +S IFEQ +S IFLT +S IFLE + +# Procedures +V CALL + +# Flow control --- these never return +V JUMP +V CJUMP +V RET + +# Special +S STACKADJUST +S GETRET +S SETRET + diff --git a/util/mcgg/ircodes.sh b/util/mcgg/ircodes.sh new file mode 100755 index 000000000..1e382e7a2 --- /dev/null +++ b/util/mcgg/ircodes.sh @@ -0,0 +1,61 @@ +#!/bin/sh + +in=$1 +header=$2 +source=$3 + +awk -f - $in >$header << "EOF" + BEGIN { + print "enum ir_opcode {" + } + + /^ *[^# ]+/ { + print "\tIR_" $2 "," + } + + END { + print "\tIR__COUNT" + print "};" + print "" + print "enum {" + print "\tIRF_SIZED = 1" + print "};" + print "" + print "extern const char* ir_names[IR__COUNT];" + print "extern const char ir_flags[IR__COUNT];" + } +EOF + +awk -f - $in >$source << "EOF" + BEGIN { + print "#include \"ircodes.h\"" + print "const char* ir_names[IR__COUNT] = {" + } + + /^ *[^# ]+/ { + printf("\t\"%s\",\n", $2) + } + + END { + print "};" + } +EOF + +awk -f - $in >>$source << "EOF" + BEGIN { + print "" + print "const char ir_flags[IR__COUNT] = {" + } + + /^ *[^# ]+/ { + if ($1 == "S") + print("\tIRF_SIZED,") + else + print("\t0,") + } + + END { + print "};" + } +EOF +