scc

Simple C99 Compiler
Log | Files | Refs | README | LICENSE

commit 289a91810b16fd9211964235252b071bf900627b
parent b08bb3cddcba51334aca7b26a31172698116cf21
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  7 May 2015 07:56:51 +0200

Remove ugly macros from cc1.h

These macros impossed too much restrictions to how the enum have to
be declared, and they didn't saved time.

Diffstat:
cc1/cc1.h | 23+++++++++++------------
cc1/expr.c | 49++++++++++++++++++++++++++++++++++++++++++-------
cc1/stmt.c | 3+--
3 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/cc1/cc1.h b/cc1/cc1.h @@ -223,13 +223,14 @@ enum { ORET, ODECL, OSWITCH, - /* TODO: This order is important, but must be changed */ - OAND, OOR, - /* - * Complementary relational operators only differ in less - * significant bit - */ - OEQ = 0x40, ONE, OLT, OGE, OLE, OGT + OAND, + OOR, + OEQ, + ONE, + OLT, + OGE, + OLE, + OGT }; /* error.c */ @@ -269,7 +270,7 @@ extern Node *symbol(Symbol *sym); extern void freetree(Node *np); /* expr.c */ -extern Node *expr(void); +extern Node *expr(void), *negate(Node *np); /* * Definition of global variables @@ -286,7 +287,4 @@ extern Type *voidtype, *pvoidtype, *booltype, *ullongtype, *llongtype, *floattype, *doubletype, *ldoubletype; -/* TODO: remove this ugly macros */ -#define NEGATE(n, v) ((n)->op ^= (v)) -#define ISNODECMP(n) ((n)->op >= OEQ) -#define ISNODELOG(n) ((n)->op >= OAND) +/* TODO: remove node(0 calls */+ \ No newline at end of file diff --git a/cc1/expr.c b/cc1/expr.c @@ -1,5 +1,6 @@ #include <inttypes.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include "../inc/cc.h" @@ -69,7 +70,7 @@ eval(Node *np) if (!np) return NULL; - if (!ISNODELOG(np)) + if (np->op != OAND && np->op != OOR) return np; p = node(0, inttype, symbol(one), symbol(zero)); return node(OASK, inttype, np, p); @@ -272,14 +273,48 @@ compare(char op, Node *np1, Node *np2) return node(op, inttype, np1, np2); } -static Node * -exp2cond(Node *np, char neg) +Node * +negate(Node *np) +{ + uint8_t op; + + switch (np->op) { + case OAND: op = OOR; break; + case OOR: op = OAND; break; + case OEQ: op = ONE; break; + case ONE: op = OEQ; break; + case OLT: op = OGE; break; + case OGE: op = OLT; break; + case OLE: op = OGT; break; + case OGT: op = OLE; break; + default: + abort(); + } + np->op = op; + return np; +} + +static bool +isnodecmp(Node *np) { - if (ISNODECMP(np)) { - NEGATE(np, neg); - return np; + switch (np->op) { + case OEQ: + case ONE: + case OLT: + case OGE: + case OLE: + case OGT: + return 1; + default: + return 0; } +} +static Node * +exp2cond(Node *np, char neg) +{ + if (isnodecmp(np)) + return (neg) ? negate(np) : np; return compare(ONE ^ neg, np, symbol(zero)); } @@ -332,7 +367,7 @@ array(Node *np1, Node *np2) Node * iszero(Node *np) { - if (ISNODECMP(np)) + if (isnodecmp(np)) return np; return compare(ONE, np, symbol(zero)); } diff --git a/cc1/stmt.c b/cc1/stmt.c @@ -293,9 +293,8 @@ If(Symbol *lbreak, Symbol *lcont, Caselist *lswitch) lelse = install("", NS_LABEL); expect(IF); np = condition(); - NEGATE(np, 1); emit(OBRANCH, lelse); - emit(OEXPR, np); + emit(OEXPR, negate(np)); stmt(lbreak, lcont, lswitch); if (accept(ELSE)) { end = install("", NS_LABEL);