commit 26e5decafcdc1a186e85805033b1f4aedb0d8667
parent 31d34ed7313e65e5f445ee38a9d96a498efc5950
Author: m21c <ho*******@gmail.com>
Date: Sun, 3 Oct 2021 21:15:59 +0200
reverted back to C-like pointer arithmetic
Diffstat:
| M | compiler.c | | | 102 | +++++++++++++++++-------------------------------------------------------------- |
1 file changed, 22 insertions(+), 80 deletions(-)
diff --git a/compiler.c b/compiler.c
@@ -76,7 +76,7 @@ enum Kind {
/* Operators */
OSUFINC, OSUFDEC, OARRAY, OCALL, ODISP,
- OLPTR, OINC, ODEC, OBNOT, OLNOT, OFLIP, /*ORPTR,*/ OPLUS, OMINUS, OCAST,
+ ODEREF, OINC, ODEC, OBNOT, OLNOT, OFLIP, OADDR, OPLUS, OMINUS, OCAST,
OMUL, ODIV, OMOD, OLSH, OARSH, ORSH, OBAND,
OADD, OSUB, OBOR, OXOR,
ORANGE,
@@ -449,10 +449,10 @@ const char *const nodestrings[] = {
[OSUFINC] = "++", [OSUFDEC] = "--",
[OARRAY] = "[]", [OCALL] = "()",
[ODISP] = ".",
- [OLPTR] = "*", [OINC] = "++",
+ [ODEREF] = "*", [OINC] = "++",
[ODEC] = "--", [OBNOT] = "~",
[OLNOT] = "!", [OFLIP] = "~=",
- /*[ORPTR] = "&",*/ [OPLUS] = "+",
+ [OADDR] = "&", [OPLUS] = "+",
[OMINUS] = "-", [OCAST] = "(type)",
[OMUL] = "*", [ODIV] = "/",
[OMOD] = "%", [OLSH] = "<<",
@@ -511,13 +511,13 @@ const uint8_t opinfo[] = {
[OCALL] = opentry(1, false, PUNSUF),
[ODISP] = opentry(1, false, PUNSUF),
- [OLPTR] = opentry(1, true, PUNARY),
+ [ODEREF] = opentry(1, true, PUNARY),
[OINC] = opentry(1, true, PUNARY),
[ODEC] = opentry(1, true, PUNARY),
[OBNOT] = opentry(1, true, PUNARY),
[OLNOT] = opentry(1, true, PUNARY),
[OFLIP] = opentry(1, true, PUNARY),
- /*[ORPTR] = opentry(1, true, PUNARY),*/
+ [OADDR] = opentry(1, true, PUNARY),
[OPLUS] = opentry(1, true, PUNARY),
[OMINUS] = opentry(1, true, PUNARY),
[OCAST] = opentry(1, true, PUNARY),
@@ -1407,8 +1407,8 @@ getunary(Kind kind)
return kind;
switch (kind) {
- case OMUL: return OLPTR;
- /* case OBAND: return ORPTR; */
+ case OMUL: return ODEREF;
+ case OBAND: return OADDR;
case OADD: return OPLUS;
case OSUB: return OMINUS;
case OSUFINC: return OINC;
@@ -2905,74 +2905,6 @@ static Node *
conv(Node *node);
static Node *
-autoref(Type *ty, Node *node)
-{
- int numderefs = 0, i;
- Node *n;
- Type *t;
-
- for (n = node; n && n->kind == OLPTR; n = n->lhs) {
- --numderefs;
-
- /*
- node->lhs = NULL;
- deletenode(node);
- */
-
- assert(n->lhs);
- node = n->lhs;
- }
-
- assert(node != NULL);
-
- /* printf("numderefs[1]: %i\n", numderefs); */
-
- for (t = node->type; t && t->kind == TPTR; t = t->target)
- ++numderefs;
-
- /* printf("numderefs[2]: %i\n", numderefs); */
-
- for (t = ty; t && t->kind == TPTR; t = t->target)
- --numderefs;
-
- /* printf("numderefs[3]: %i\n", numderefs); */
-
-#if 0
- if (numderefs)
- node = conv(node);
-#endif
-
- for (i = 0; i < numderefs; ++i) {
- n = makenode(node, node);
- n->type = node->type->target;
- n->kind = ADEREF; /* dereference */
- node = n;
- }
-
- if (numderefs == -1) {
- t = maketype(&node->loc, prim + TPTR, node->type);
-
- n = makenode(node, node);
- n->type = t;
- n->kind = AADDR; /* address-of */
- node = n;
-
- /* TODO(m21c): Check for lvalue */
- } else if (numderefs < -1) {
- error(&node->loc, "double referencing");
- /* TODO(m21c): ERROR: double referencing */
-
- return node;
- }
-
- if (ty && numderefs) {
- /* TODO(m21c): Check for type-compatability and for ptr-types */
- }
-
- return node;
-}
-
-static Node *
wrap(Type *ty, Node *node)
{
assert(ty);
@@ -3022,8 +2954,6 @@ wrap(Type *ty, Node *node)
return node;
}
- node = autoref(ty, node);
-
node = makenode(node, node);
node->kind = ACONV;
node->type = ty;
@@ -3043,7 +2973,7 @@ conv(Node *node)
if (ty->kind == TUINFER)
return wrap(prim + TUINT, node);
- return autoref(NULL, node);
+ return node;
}
@@ -3169,8 +3099,19 @@ typecheck(Env *env, Node *expr)
return expr;
switch (expr->kind) {
- case OLPTR:
+ case ODEREF:
expr->type = expr->lhs->type;
+
+ if (expr->type->kind != TPTR) {
+ error(&expr->loc, "operand is not a pointer");
+ } else {
+ expr->type = expr->type->target;
+ }
+
+ return expr;
+
+ case OADDR:
+ expr->type = maketype(&expr->loc, prim + TPTR, expr->lhs->type);
return expr;
case OPLUS: case OMINUS:
@@ -4168,7 +4109,8 @@ printexpr(FILE *out, Node *expr, int indent)
n += highlight(out, HLDELIM);
n += fprintf(out, "%s", nodestrings[expr->kind]);
if (getprec(expr->lhs->kind) == PUNARY &&
- expr->kind != OLPTR)
+ expr->kind != ODEREF &&
+ expr->kind != OADDR)
{
putc(' ', out), ++n;
}