From: mike lowis Date: Mon, 3 May 2021 03:20:43 +0000 (-0400) Subject: checkpoint commit X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=43dbf11eb4df4bb0e6aeeba7b095fb6ddf6ae0c9;p=proto%2Fobnc.git checkpoint commit --- diff --git a/cerise/examples/operators.c b/cerise/examples/operators.c index c9cdbf6..e1371c8 100644 --- a/cerise/examples/operators.c +++ b/cerise/examples/operators.c @@ -1,5 +1,3 @@ -long A, B; - void pos(void) { A = +A; } void neg(void) { A = -A; } @@ -27,3 +25,31 @@ void bor(void) { A = A | B; } void bnot(void) { A = ~A; } void bxor(void) { A = A ^ B; } +long A, B; + +long addi(void) { return A + B; } +//long addii(void) { return A + 3; } + +long subi(void) { return A - B; } +//long subii(void) { return A - 3; } + +long muli(void) { return A * B; } +//long mulii(void) { return A * 3; } + +long divi(void) { return A / B; } +//long divii(void) { return A / 3; } + +long modi(void) { return A % B; } +//long modii(void) { return A % 3; } + +void pop(void) { asm("pop %rax"); } +void push(void) { asm("push %rax"); } + +// call +// ret + +long ldi(void) { return 42; } +long ldm(void) { return A; } +void sti(void) { A = 42; } +void stm(void) { A = B; } + diff --git a/cerise/examples/x86_64_ops.s b/cerise/examples/x86_64_ops.s new file mode 100644 index 0000000..9757677 --- /dev/null +++ b/cerise/examples/x86_64_ops.s @@ -0,0 +1,125 @@ +pop: + pop %rax +push: + push %rax + +ldi: + movl $42, %eax +ldm: + movq A(%rip), %rax +sti: + movq $42, A(%rip) +stm: + movq %rax, A(%rip) + +neg: + negq A(%rip) + negq %rax +add: + addq A(%rip), %rax + addq $42, %rax +sub: + subq B(%rip), %rax + subq $42, %rax +mul: + imulq B(%rip), %rax + imulq $42, %rax +div: + cqto # FIRST + idivq B(%rip) + idivq %rax +mod: + cqto # FIRST + idivq B(%rip) + idivq %rax + movq %rdx, %rax # LAST + +eq: + movq B(%rip), %rax + cmpq %rax, A(%rip) + sete %al + movzbl %al, %eax +neq: + movq B(%rip), %rax + cmpq %rax, A(%rip) + setne %al + movzbl %al, %eax +lt: + movq B(%rip), %rax + cmpq %rax, A(%rip) + setl %al + movzbl %al, %eax +lte: + movq B(%rip), %rax + cmpq %rax, A(%rip) + setle %al + movzbl %al, %eax +gt: + movq B(%rip), %rax + cmpq %rax, A(%rip) + setg %al + movzbl %al, %eax +gte: + movq B(%rip), %rax + cmpq %rax, A(%rip) + setge %al + movzbl %al, %eax +eqi: + xorl %eax, %eax + cmpq $4, A(%rip) + sete %al +neqi: + xorl %eax, %eax + cmpq $4, A(%rip) + setne %al +lti: + xorl %eax, %eax + cmpq $3, A(%rip) + setle %al +ltei: + xorl %eax, %eax + cmpq $4, A(%rip) + setle %al +gti: + xorl %eax, %eax + cmpq $4, A(%rip) + setg %al +gtei: + xorl %eax, %eax + cmpq $3, A(%rip) + setg %al + +shr: + movq B(%rip), %rcx + sarq %cl, A(%rip) + ret +shl: + movq B(%rip), %rcx + salq %cl, A(%rip) + ret +band: + movq B(%rip), %rax + andq %rax, A(%rip) + ret +bor: + movq B(%rip), %rax + orq %rax, A(%rip) + ret +bnot: + notq A(%rip) + ret +bxor: + movq B(%rip), %rax + xorq %rax, A(%rip) + ret + +shri: + sarq $4, A(%rip) +shli: + salq $4, A(%rip) +bandi: + andq $4, A(%rip) +bori: + orq $4, A(%rip) +bxori: + xorq $4, A(%rip) diff --git a/cerise/oberon0/OSP.Mod b/cerise/oberon0/OSP.Mod index 4185e42..2997d6a 100644 --- a/cerise/oberon0/OSP.Mod +++ b/cerise/oberon0/OSP.Mod @@ -61,7 +61,8 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) END OpenScope; PROCEDURE CloseScope; - BEGIN topScope := topScope.dsc + BEGIN + topScope := topScope.dsc END CloseScope; (* -------------------- Parser ---------------------*) @@ -79,7 +80,8 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) END ; Check(OSS.rbrak, "no ]") - ELSE (*period*) OSS.Get(sym); + ELSE (*period*) + OSS.Get(sym); IF sym = OSS.ident THEN IF x.type.form = OSG.Record THEN FindField(obj, x.type.dsc); OSS.Get(sym); @@ -100,7 +102,8 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) PROCEDURE Parameter(par: OSG.Object); VAR x: OSG.Item; varpar: BOOLEAN; - BEGIN expression(x); + BEGIN + expression(x); IF par # NIL THEN varpar := par.class = OSG.Par; IF CompTypes(par.type, x.type) THEN @@ -119,17 +122,24 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) VAR n: INTEGER; par: OSG.Object; BEGIN par := obj.dsc; n := 0; IF sym # OSS.rparen THEN - Parameter(par); n := 1; + Parameter(par); + n := 1; WHILE sym <= OSS.comma DO Check(sym, "comma?"); - IF par # NIL THEN par := par.next END ; - INC(n); Parameter(par) + IF par # NIL THEN + par := par.next + END ; + INC(n); + Parameter(par) END ; Check(OSS.rparen, ") missing") - ELSE OSS.Get(sym); + ELSE + OSS.Get(sym); END ; - IF n < obj.nofpar THEN OSS.Mark("too few params") - ELSIF n > obj.nofpar THEN OSS.Mark("too many params") + IF n < obj.nofpar THEN + OSS.Mark("too few params") + ELSIF n > obj.nofpar THEN + OSS.Mark("too many params") END END ParamList; @@ -145,7 +155,7 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) IF sym = OSS.rparen THEN OSS.Get(sym) ELSE OSS.Mark("rparen expected") END ELSE OSS.Mark("param missing"); OSG.MakeConstItem(x, OSG.intType, 0) END - END StandFunc; + END StandF\unc; PROCEDURE factor(VAR x: OSG.Item); VAR obj: OSG.Object; @@ -154,6 +164,7 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) OSS.Mark("expression expected"); REPEAT OSS.Get(sym) UNTIL (sym >= OSS.int) & (sym <= OSS.ident) END ; + IF sym = OSS.ident THEN find(obj); OSS.Get(sym); @@ -167,27 +178,36 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) OSG.MakeItem(x, obj, level); selector(x) END + ELSIF sym = OSS.int THEN OSG.MakeConstItem(x, OSG.intType, OSS.val); OSS.Get(sym) + ELSIF sym = OSS.char THEN OSG.MakeConstItem(x, OSG.intType, OSS.val); OSS.Get(sym) + ELSIF sym = OSS.lparen THEN OSS.Get(sym); - IF sym # OSS.rparen THEN expression(x) END ; + IF sym # OSS.rparen THEN + expression(x) + END ; Check(OSS.rparen, "no )") + ELSIF sym = OSS.not THEN OSS.Get(sym); factor(x); CheckBool(x); OSG.Not(x) + ELSIF sym = OSS.false THEN OSS.Get(sym); OSG.MakeConstItem(x, OSG.boolType, 0) + ELSIF sym = OSS.true THEN OSS.Get(sym); OSG.MakeConstItem(x, OSG.boolType, 1) + ELSE OSS.Mark("factor?"); OSG.MakeItem(x, dummy, level) @@ -591,7 +611,8 @@ MODULE OSP; (* NW 23.9.93 / 9,5.2017 OSPX*) END END FPSection; - BEGIN (* ProcedureDecl *) OSS.Get(sym); + BEGIN (* ProcedureDecl *) + OSS.Get(sym); IF sym = OSS.ident THEN procid := OSS.id; NewObj(proc, OSG.Proc); OSS.Get(sym); parblksize := marksize; nofpar := 0; (* Texts.Write(W, "%"); Texts.WriteInt(W, sym, 4); Texts.WriteLn(W); Texts.Append(Oberon.Log, W.buf); *) diff --git a/cerise/src/grammar.c b/cerise/src/grammar.c index 5503ca3..1d02b99 100644 --- a/cerise/src/grammar.c +++ b/cerise/src/grammar.c @@ -61,17 +61,23 @@ static void init_item(Item* item, Symbol* sym) *****************************************************************************/ static void expression(Parser* p, Item* item); -//RULE(expr_list) -//{ -// while (1) -// { -// expression(p); -// if (!accept(p, ',')) -// { -// break; -// } -// } -//} +RULE(expr_list) +{ + (void)item; + int nargs = 0; + Item arg = {0}; + if (!matches(p, ')')) + { + expression(p, &arg); + nargs++; + while (!matches(p, ')')) + { + expect(p, ','); + expression(p, &arg); + nargs++; + } + } +} RULE(qualident) { @@ -103,9 +109,9 @@ RULE(qualident) RULE(designator) { qualident(p, item); -// /* selector */ -// switch ((int)peek(p)->type) -// { + /* selector */ + switch ((int)peek(p)->type) + { // case '.': // expect(p, IDENT); // break; @@ -123,7 +129,7 @@ RULE(designator) // qualident(p); // expect(p, ')'); // break; -// } + } } RULE(factor) @@ -170,7 +176,14 @@ RULE(factor) case IDENT: designator(p, item); -// actual_params(p); + if (accept(p, '(')) + { + //Item call = {0}; + // check item is a procedure + expr_list(p, item); + //codegen_call(p, &call); + expect(p, ')'); + } break; } } diff --git a/cerise/tests/Module.m b/cerise/tests/Module.m index ca2764d..00e3193 100644 --- a/cerise/tests/Module.m +++ b/cerise/tests/Module.m @@ -55,30 +55,32 @@ begin # c = b + b * b + b; # - # If statements - if 1 == 1 then - c = 1; - end - - if 1 == 1 then - c = 1; - else - c = 1; - end - - if 1 == 1 then - c = 1; - elsif 2 == 2 then - c = 1; - end +# # If statements +# if 1 == 1 then +# c = 1; +# end +# +# if 1 == 1 then +# c = 1; +# else +# c = 1; +# end +# +# if 1 == 1 then +# c = 1; +# elsif 2 == 2 then +# c = 1; +# end +# +# if 1 == 1 then +# c = 1; +# elsif 2 == 2 then +# c = 2; +# elsif 2 == 2 then +# c = 2; +# else +# c = 3; +# end - if 1 == 1 then - c = 1; - elsif 2 == 2 then - c = 2; - elsif 2 == 2 then - c = 2; - else - c = 3; - end + c = c(1,2,3); end