changeset 502:14a40f8bb4eb

Add various operators to lwcc Add various binary and ternary operators to lwcc, but only those which can work with constant operands. Seems like variables are probably required next.
author William Astle <lost@l-w.ca>
date Wed, 25 Sep 2019 20:23:49 -0600
parents f3e9732973f1
children 17fa03d69df3
files lwcc/cc-gencode.c lwcc/cc-parse.c lwcc/tree.c lwcc/tree.h
diffstat 4 files changed, 349 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lwcc/cc-gencode.c	Tue Sep 24 22:07:56 2019 -0600
+++ b/lwcc/cc-gencode.c	Wed Sep 25 20:23:49 2019 -0600
@@ -27,9 +27,20 @@
 
 #include "tree.h"
 
+char *generate_nextlabel(void)
+{
+    static int labelnum = 0;
+    char buf[16];
+    
+    sprintf(buf, "L%d", labelnum++);
+    return lw_strdup(buf);
+}
+
 void generate_code(node_t *n, FILE *output)
 {
     node_t *nn;
+    char *label1, *label2;
+
     switch (n -> type)
     {
     // function definition - output prologue, then statements, then epilogue
@@ -70,7 +81,99 @@
         generate_code(n->children->next_child, output);
         fprintf(output, "\tjsr ___div16i\n");
         break;
+    
+    case NODE_OPER_MOD:
+        generate_code(n -> children, output);
+        fprintf(output, "\tpshs d\n");
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\tjsr ___mod16i\n");
+        break;
 
+    case NODE_OPER_COND:
+        label1 = generate_nextlabel();
+        label2 = generate_nextlabel();
+        generate_code(n -> children, output);
+        fprintf(output, "\tsubd #0\n\tbeq %s\n", label1);
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\tbra %s\n%s\n", label2, label1);
+        generate_code(n -> children -> next_child -> next_child, output);
+        fprintf(output, "%s\n", label2);
+        lw_free(label1);
+        lw_free(label2);
+        break;
+    
+    case NODE_OPER_COMMA:
+        generate_code(n -> children, output);
+        generate_code(n -> children -> next_child, output);
+        break;
+    
+    case NODE_OPER_BWAND:
+        generate_code(n -> children, output);
+        fprintf(output, "\tpshs d\n");
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\tandb 1,s\n\tanda ,s++\n");
+        break;
+
+    case NODE_OPER_BWOR:
+        generate_code(n -> children, output);
+        fprintf(output, "\tpshs d\n");
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\torb 1,s\n\tora ,s++\n");
+        break;
+
+    case NODE_OPER_BWXOR:
+        generate_code(n -> children, output);
+        fprintf(output, "\tpshs d\n");
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\teorb 1,s\n\teora ,s++\n");
+        break;
+
+    case NODE_OPER_BAND:
+        label1 = generate_nextlabel();
+        generate_code(n -> children, output);
+        fprintf(output, "\tsubd #0\n\tbeq %s\n", label1);
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\tsubd #0\n\tbeq %s\n\tldd #1\n%s\n", label1, label1);
+        lw_free(label1);
+        break;
+
+    case NODE_OPER_BOR:
+        label1 = generate_nextlabel();
+        label2 = generate_nextlabel();
+        generate_code(n -> children, output);
+        fprintf(output, "\tsubd #0\n\tbne %s\n", label1);
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\tsubd #0\n\tbeq %s\n%s\tldd #1\n%s\n", label2, label1, label2);
+        lw_free(label1);
+        lw_free(label2);
+        break; 
+
+    case NODE_OPER_NE:
+    case NODE_OPER_EQ:
+    case NODE_OPER_LT:
+    case NODE_OPER_GT:
+    case NODE_OPER_LE:
+    case NODE_OPER_GE:
+        generate_code(n -> children, output);
+        fprintf(output, "\tpshs d\n");
+        generate_code(n -> children -> next_child, output);
+        fprintf(output, "\tsubd ,s++\n");
+        label1 = generate_nextlabel();
+        label2 = generate_nextlabel();
+        fprintf(output, "\t%s %s\n", (
+            (n -> type == NODE_OPER_NE ? "bne" :
+            (n -> type == NODE_OPER_EQ ? "beq" :
+            (n -> type == NODE_OPER_LT ? "bge" :
+            (n -> type == NODE_OPER_GT ? "ble" :
+            (n -> type == NODE_OPER_LE ? "bgt" :
+            (n -> type == NODE_OPER_GE ? "blt" :
+            "foobar"))))))
+        ), label1);
+        fprintf(output, "\tldd #0\n\tbra %s\n%s\tldd #1\n%s\n", label2, label1, label2);
+        lw_free(label1);
+        lw_free(label2);
+        break;
+    
     default:
         for (nn = n -> children; nn; nn = nn -> next_child)
             generate_code(nn, output);
--- a/lwcc/cc-parse.c	Tue Sep 24 22:07:56 2019 -0600
+++ b/lwcc/cc-parse.c	Wed Sep 25 20:23:49 2019 -0600
@@ -169,6 +169,8 @@
 
 }
 
+node_t *parse_expr_real(struct parser_state *ps, int prec);
+
 node_t *parse_term_real(struct parser_state *ps)
 {
     node_t *rv;
@@ -185,13 +187,125 @@
     return NULL;
 }
 
+node_t *parse_expr_fncall(struct parser_state *ps, node_t *term1)
+{
+    if (ps -> curtok -> ttype != TOK_CPAREN)
+    {
+        node_destroy(term1);
+        parse_generr(ps, "missing )");
+        return NULL;
+    }
+    parse_next(ps);
+    return node_create(NODE_OPER_FNCALL, term1, NULL);
+}
+
+node_t *parse_expr_postinc(struct parser_state *ps, node_t *term1)
+{
+    return node_create(NODE_OPER_POSTINC, term1);
+}
+
+node_t *parse_expr_postdec(struct parser_state *ps, node_t *term1)
+{
+    return node_create(NODE_OPER_POSTDEC, term1);
+}
+
+node_t *parse_expr_subscript(struct parser_state *ps, node_t *term1)
+{
+    node_t *term2;
+    term2 = parse_expr_real(ps, 0);
+    if (!term2)
+    {
+        node_destroy(term1);
+        return NULL;
+    }
+    if (ps -> curtok -> ttype != TOK_CSQUARE)
+    {
+        node_destroy(term2);
+        node_destroy(term1);
+        parse_generr(ps, "missing ]");
+        return NULL;
+    }
+    parse_next(ps);
+    return node_create(NODE_OPER_SUBSCRIPT, term1, term2);
+}
+
+node_t *parse_expr_cond(struct parser_state *ps, node_t *term1)
+{
+    node_t *term2, *term3;
+    // conditional operator
+    // NOTE: the middle operand is evaluated as though it is its own
+    // independent expression because the : must appear. The third
+    // operand is evaluated at the ternary operator precedence so that
+    // subsequent operand binding behaves correctly (if surprisingly). This
+    // would be less confusing if the ternary operator was fully bracketed
+    // (that is, had a terminator)
+    term2 = parse_expr_real(ps, 0);
+    if (!term2)
+    {
+        node_destroy(term1);
+        return NULL;
+    }
+    if (ps -> curtok -> ttype == TOK_COLON)
+    {
+        parse_next(ps);
+        term3 = parse_expr_real(ps, 25);
+        if (!term3)
+        {
+            node_destroy(term1);
+            node_destroy(term2);
+            return NULL;
+        }
+        return node_create(NODE_OPER_COND, term1, term2, term3);
+    }
+    else
+    {
+        node_destroy(term1);
+        node_destroy(term2);
+        parse_generr(ps, "missing :");
+        return NULL;
+    }
+}
+
 node_t *parse_expr_real(struct parser_state *ps, int prec)
 {
-    static struct { int tok; int nodetype; int prec; } operlist[] = {
+    static struct { int tok; int nodetype; int prec; int ra; node_t *(*spec)(struct parser_state *, node_t *); } operlist[] = {
+//        { TOK_OPAREN, NODE_OPER_FNCALL, 200, 0, parse_expr_fncall },
+//        { TOK_OSQUARE, NODE_OPER_SUBSCRIPT, 200, 0, parse_expr_subscript },
+//        { TOK_ARROW, NODE_OPER_PTRMEM, 200, 0 },
+//        { TOK_DOT, NODE_OPER_OBJMEM, 200, 0 },
+//        { TOK_DBLADD, NODE_OPER_POSTINC, 200, 0, parse_expr_postinc },
+//        { TOK_DBLSUB, NODE_OPER_POSTDEC, 200, 0, parse_expr_postdec },
         { TOK_STAR, NODE_OPER_TIMES, 150 },
         { TOK_DIV, NODE_OPER_DIVIDE, 150 },
+        { TOK_MOD, NODE_OPER_MOD, 150 },
         { TOK_ADD, NODE_OPER_PLUS, 100 },
         { TOK_SUB, NODE_OPER_MINUS, 100 },
+        { TOK_LSH, NODE_OPER_LSH, 90 },
+        { TOK_RSH, NODE_OPER_RSH, 90 },
+        { TOK_LT, NODE_OPER_LT, 80 },
+        { TOK_LE, NODE_OPER_LE, 80 },
+        { TOK_GT, NODE_OPER_GT, 80 },
+        { TOK_GE, NODE_OPER_GE, 80 },
+        { TOK_EQ, NODE_OPER_EQ, 70 },
+        { TOK_NE, NODE_OPER_NE, 70 },
+        { TOK_BWAND, NODE_OPER_BWAND, 60},
+        { TOK_XOR, NODE_OPER_BWXOR, 55 },
+        { TOK_BWOR, NODE_OPER_BWOR, 50 },
+        { TOK_BAND, NODE_OPER_BAND, 40 },
+        { TOK_BOR, NODE_OPER_BOR, 35 },
+        { TOK_QMARK, NODE_OPER_COND, 25, 1, parse_expr_cond },
+//        { TOK_ASS, NODE_OPER_ASS, 20, 1 },
+//        { TOK_ADDASS, NODE_OPER_ADDASS, 20, 1 },
+//        { TOK_SUBASS, NODE_OPER_SUBASS, 20, 1 },
+//        { TOK_MULASS, NODE_OPER_MULASS, 20, 1 },
+//        { TOK_DIVASS, NODE_OPER_DIVASS, 20, 1 },
+//        { TOK_MODASS, NODE_OPER_MODASS, 20, 1 },
+//        { TOK_LSHASS, NODE_OPER_LSHASS, 20, 1 },
+//        { TOK_RSHASS, NODE_OPER_RSHASS, 20, 1 },
+//        { TOK_BWANDASS, NODE_OPER_BWANDASS, 20, 1},
+//        { TOK_BWORASS, NODE_OPER_BWORASS, 20, 1 },
+//        { TOK_XORASS, NODE_OPER_BWXORASS, 20, 1 },
+        { TOK_COMMA, NODE_OPER_COMMA, 1 },
         { 0, 0, 0 }
     };
     node_t *term1, *term2;
@@ -210,16 +324,35 @@
     if (operlist[i].tok == 0)
         return term1;
 
-    // is the next operator less or same precedence?
-    if (operlist[i].prec <= prec)
+    // is previous operation higher precedence? If so, just return the first term
+    if (operlist[i].prec < prec)
         return term1;
 
+    // is this operator left associative and previous operation is same precedence?
+    // if so, just return the first term
+    if (operlist[i].ra == 0 && operlist[i].prec == prec)
+        return term1;
+
+    // consume the operator
     parse_next(ps);
+    
+    // special handling
+    if (operlist[i].spec)
+    {
+        term2 = (operlist[i].spec)(ps, term1);
+        if (!term2)
+        {
+            node_destroy(term1);
+            return NULL;
+        }
+        term1 = term2;
+        goto nextoper;
+    }
     term2 = parse_expr_real(ps, operlist[i].prec);
     if (!term2)
     {
         parse_generr(ps, "expr");
-        node_destroy(term2);
+        node_destroy(term1);
     }
     
     term1 = node_create(operlist[i].nodetype, term1, term2);
--- a/lwcc/tree.c	Tue Sep 24 22:07:56 2019 -0600
+++ b/lwcc/tree.c	Wed Sep 25 20:23:49 2019 -0600
@@ -57,6 +57,39 @@
 	"OPER_MINUS",
 	"OPER_TIMES",
 	"OPER_DIVIDE",
+	"OPER_MOD",
+	"OPER_COND",
+	"OPER_FNCALL",
+	"OPER_SUBSCRIPT",
+	"OPER_POSTINC",
+	"OPER_POSTDEC",
+	"OPER_PTRMEM",
+	"OPER_OBJMEM",
+	"OPER_LSH",
+	"OPER_RSH",
+	"OPER_LT",
+	"OPER_LE",
+	"OPER_GT",
+	"OPER_GE",
+	"OPER_EQ",
+	"OPER_NE",
+	"OPER_BWAND",
+	"OPER_BWXOR",
+	"OPER_BWOR",
+	"OPER_BAND",
+	"OPER_BOR",
+	"OPER_ASS",
+	"OPER_ADDASS",
+	"OPER_SUBASS",
+	"OPER_MULASS",
+	"OPER_DIVASS",
+	"OPER_MODASS",
+	"OPER_LSHASS",
+	"OPER_RSHASS",
+	"OPER_BWANDASS",
+	"OPER_BWXORASS",
+	"OPER_BWORASS",
+	"OPER_COMMA",
 };
 
 
@@ -78,12 +111,50 @@
 	case NODE_OPER_MINUS:
 	case NODE_OPER_TIMES:
 	case NODE_OPER_DIVIDE:
+	case NODE_OPER_MOD:
+	case NODE_OPER_BWAND:
+	case NODE_OPER_BWXOR:
+	case NODE_OPER_BWOR:
+	case NODE_OPER_BAND:
+	case NODE_OPER_BOR:
+	case NODE_OPER_SUBSCRIPT:
+	case NODE_OPER_PTRMEM:
+	case NODE_OPER_OBJMEM:
+	case NODE_OPER_ASS:
+	case NODE_OPER_ADDASS:
+	case NODE_OPER_SUBASS:
+	case NODE_OPER_MULASS:
+	case NODE_OPER_DIVASS:
+	case NODE_OPER_MODASS:
+	case NODE_OPER_LSH:
+	case NODE_OPER_LSHASS:
+	case NODE_OPER_RSH:
+	case NODE_OPER_RSHASS:
+	case NODE_OPER_BWANDASS:
+	case NODE_OPER_BWORASS:
+	case NODE_OPER_BWXORASS:
+	case NODE_OPER_LT:
+	case NODE_OPER_LE:
+	case NODE_OPER_GT:
+	case NODE_OPER_GE:
+	case NODE_OPER_EQ:
+	case NODE_OPER_NE:
+	case NODE_OPER_COMMA:
+		nargs = 2;
+		break;
+
+	case NODE_OPER_FNCALL:
 		nargs = 2;
 		break;
 
 	case NODE_DECL:
 		nargs = 2;
 		break;
+
+	case NODE_OPER_POSTINC:
+	case NODE_OPER_POSTDEC:
+		nargs = 1;
+		break;
 	
 	case NODE_TYPE_PTR:
 		nargs = 1;
@@ -98,6 +169,10 @@
 		nargs = 4;
 		break;
 	
+	case NODE_OPER_COND:
+		nargs = 3;
+		break;
+	
 	case NODE_FUNDECL:
 		nargs = 3;
 		break;
--- a/lwcc/tree.h	Tue Sep 24 22:07:56 2019 -0600
+++ b/lwcc/tree.h	Wed Sep 25 20:23:49 2019 -0600
@@ -55,7 +55,40 @@
 #define NODE_OPER_MINUS     27  // subtraction
 #define NODE_OPER_TIMES     28  // multiplcation
 #define NODE_OPER_DIVIDE    29  // division
-#define NODE_NUMTYPES		30	// the number of node types
+#define NODE_OPER_MOD       30  // modulus
+#define NODE_OPER_COND      31  // ternary conditional
+#define NODE_OPER_FNCALL    32  // function call
+#define NODE_OPER_SUBSCRIPT 33  // array subscript
+#define NODE_OPER_POSTINC   34  // post increment
+#define NODE_OPER_POSTDEC   35  // post decrement
+#define NODE_OPER_PTRMEM    36  // member of pointer operator
+#define NODE_OPER_OBJMEM    37  // member of object operator
+#define NODE_OPER_LSH       38  // left shift
+#define NODE_OPER_RSH       39  // right shift
+#define NODE_OPER_LT        40  // less than
+#define NODE_OPER_LE        41  // less than or equal
+#define NODE_OPER_GT        42  // greater than
+#define NODE_OPER_GE        43  // greater than or equal
+#define NODE_OPER_EQ        44  // equality
+#define NODE_OPER_NE        45  // inequality
+#define NODE_OPER_BWAND     46  // bitwise and
+#define NODE_OPER_BWXOR     47  // bitwise xor
+#define NODE_OPER_BWOR      48  // bitwise or
+#define NODE_OPER_BAND      49  // boolean and
+#define NODE_OPER_BOR       50  // boolean or
+#define NODE_OPER_ASS       51  // assignment
+#define NODE_OPER_ADDASS    52  // add/assign combo
+#define NODE_OPER_SUBASS    53  // subtract/assign combo
+#define NODE_OPER_MULASS    54  // multiply/assign combo
+#define NODE_OPER_DIVASS    55  // divide/assign combo
+#define NODE_OPER_MODASS    56  // modulus/assign combo
+#define NODE_OPER_LSHASS    57  // left shift/assign combo
+#define NODE_OPER_RSHASS    58  // right shift/assign combo
+#define NODE_OPER_BWANDASS  59  // bitwise and/assign combo
+#define NODE_OPER_BWXORASS  60  // bitwise xor/assign combo
+#define NODE_OPER_BWORASS   61  // bitwise or/assign combo
+#define NODE_OPER_COMMA     62  // comma sequential evaluation operator
+#define NODE_NUMTYPES		63	// the number of node types
 
 typedef struct node_s node_t;