History

expression optimization

bellard committed 23 years ago

Commit a9f08651 parent 9e180471 file · +119 −29Browse files

Unified · Split

tcc.c+119−29
@@ -553,7 +553,6 @@ extern long double strtold (const char *__nptr, char **__endptr); · 552 unmodified lines
553553 static char *pstrcpy(char *buf, int buf_size, const char *s);
554554 static char *pstrcat(char *buf, int buf_size, const char *s);
555555
556−static void sum(int l);
557556 static void next(void);
558557 static void next_nomacro(void);
559558 static void parse_expr_type(CType *type);
@@ -6478,39 +6477,130 @@ static void uneq(void) · 5918 unmodified lines
64786477 }
64796478 }
64806479
6481−static void sum(int l)
6480+static void expr_prod(void)
64826481 {
64836482 int t;
64846483
6485− if (l == 0)
6484+ uneq();
6485+ while (tok == '*' || tok == '/' || tok == '%') {
6486+ t = tok;
6487+ next();
64866488 uneq();
6487− else {
6488− sum(--l);
6489− while ((l == 0 && (tok == '*' || tok == '/' || tok == '%')) ||
6490− (l == 1 && (tok == '+' || tok == '-')) ||
6491− (l == 2 && (tok == TOK_SHL || tok == TOK_SAR)) ||
6492− (l == 3 && ((tok >= TOK_ULE && tok <= TOK_GT) ||
6493− tok == TOK_ULT || tok == TOK_UGE)) ||
6494− (l == 4 && (tok == TOK_EQ || tok == TOK_NE)) ||
6495− (l == 5 && tok == '&') ||
6496− (l == 6 && tok == '^') ||
6497− (l == 7 && tok == '|') ||
6498− (l == 8 && tok == TOK_LAND) ||
6499− (l == 9 && tok == TOK_LOR)) {
6500− t = tok;
6501− next();
6502− sum(l);
6503− gen_op(t);
6504− }
6489+ gen_op(t);
6490+ }
6491+}
6492+
6493+static void expr_sum(void)
6494+{
6495+ int t;
6496+
6497+ expr_prod();
6498+ while (tok == '+' || tok == '-') {
6499+ t = tok;
6500+ next();
6501+ expr_prod();
6502+ gen_op(t);
6503+ }
6504+}
6505+
6506+static void expr_shift(void)
6507+{
6508+ int t;
6509+
6510+ expr_sum();
6511+ while (tok == TOK_SHL || tok == TOK_SAR) {
6512+ t = tok;
6513+ next();
6514+ expr_sum();
6515+ gen_op(t);
6516+ }
6517+}
6518+
6519+static void expr_cmp(void)
6520+{
6521+ int t;
6522+
6523+ expr_shift();
6524+ while ((tok >= TOK_ULE && tok <= TOK_GT) ||
6525+ tok == TOK_ULT || tok == TOK_UGE) {
6526+ t = tok;
6527+ next();
6528+ expr_shift();
6529+ gen_op(t);
6530+ }
6531+}
6532+
6533+static void expr_cmpeq(void)
6534+{
6535+ int t;
6536+
6537+ expr_cmp();
6538+ while (tok == TOK_EQ || tok == TOK_NE) {
6539+ t = tok;
6540+ next();
6541+ expr_cmp();
6542+ gen_op(t);
6543+ }
6544+}
6545+
6546+static void expr_and(void)
6547+{
6548+ expr_cmpeq();
6549+ while (tok == '&') {
6550+ next();
6551+ expr_cmpeq();
6552+ gen_op('&');
6553+ }
6554+}
6555+
6556+static void expr_xor(void)
6557+{
6558+ expr_and();
6559+ while (tok == '^') {
6560+ next();
6561+ expr_and();
6562+ gen_op('^');
6563+ }
6564+}
6565+
6566+static void expr_or(void)
6567+{
6568+ expr_xor();
6569+ while (tok == '|') {
6570+ next();
6571+ expr_xor();
6572+ gen_op('|');
6573+ }
6574+}
6575+
6576+/* XXX: suppress this mess */
6577+static void expr_land_const(void)
6578+{
6579+ expr_or();
6580+ while (tok == TOK_LAND) {
6581+ next();
6582+ expr_or();
6583+ gen_op(TOK_LAND);
6584+ }
6585+}
6586+
6587+/* XXX: suppress this mess */
6588+static void expr_lor_const(void)
6589+{
6590+ expr_land_const();
6591+ while (tok == TOK_LOR) {
6592+ next();
6593+ expr_land_const();
6594+ gen_op(TOK_LOR);
65056595 }
65066596 }
65076597
65086598 /* only used if non constant */
6509−static void eand(void)
6599+static void expr_land(void)
65106600 {
65116601 int t;
65126602
6513− sum(8);
6603+ expr_or();
65146604 t = 0;
65156605 while (1) {
65166606 if (tok != TOK_LAND) {
@@ -6522,15 +6612,15 @@ static void eand(void) · 5 unmodified lines
65226612 }
65236613 t = gtst(1, t);
65246614 next();
6525− sum(8);
6615+ expr_or();
65266616 }
65276617 }
65286618
6529−static void eor(void)
6619+static void expr_lor(void)
65306620 {
65316621 int t;
65326622
6533− eand();
6623+ expr_land();
65346624 t = 0;
65356625 while (1) {
65366626 if (tok != TOK_LOR) {
@@ -6542,7 +6632,7 @@ static void eor(void) · 5 unmodified lines
65426632 }
65436633 t = gtst(0, t);
65446634 next();
6545− eand();
6635+ expr_land();
65466636 }
65476637 }
65486638
@@ -6555,7 +6645,7 @@ static void expr_eq(void) · 6 unmodified lines
65556645
65566646 if (const_wanted) {
65576647 int c1, c;
6558− sum(10);
6648+ expr_lor_const();
65596649 if (tok == '?') {
65606650 c = vtop->c.i;
65616651 vpop();
@@ -6569,7 +6659,7 @@ static void expr_eq(void) · 7 unmodified lines
65696659 vtop->c.i = c1;
65706660 }
65716661 } else {
6572− eor();
6662+ expr_lor();
65736663 if (tok == '?') {
65746664 next();
65756665 save_regs(1); /* we need to save all registers here except

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t