Skip to content

Commit

Permalink
feat: impl pointer calc
Browse files Browse the repository at this point in the history
  • Loading branch information
togami2864 committed Nov 4, 2021
1 parent 8cf8ae9 commit f33a74f
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 8 deletions.
13 changes: 7 additions & 6 deletions 50cc.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ bool startswith(char *p, char *q);
Token *new_token(TokenKind kind, Token *cur, char *str, int len);
Token *tokenize();

typedef struct Type Type;
struct Type {
enum { INT, PTR } ty;
struct Type *ptr_to;
};

typedef enum {
ND_ADD,
ND_SUB,
Expand Down Expand Up @@ -88,12 +94,7 @@ struct Node {
int len;
int val;
int offset;
};

typedef struct Type Type;
struct Type {
enum { INT, PTR } ty;
struct Type *ptr_to;
Type *type;
};

extern Node *code[];
Expand Down
Binary file modified codegen.o
Binary file not shown.
9 changes: 9 additions & 0 deletions func/func.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <stdlib.h>

int foo() { printf("%d\n", 10); }

Expand All @@ -7,4 +8,12 @@ int fooo(int x, int y) { printf("%d\n", x + y); }
int foooo(int x, int y, int z) {
printf("%d\n", z + y + z);
return x + y + z;
}

void alloc(int **p, int a, int b, int c, int d) {
*p = malloc(sizeof(int) * 4);
(*p)[0] = a;
(*p)[1] = b;
(*p)[2] = c;
(*p)[3] = d;
}
Binary file modified func/func.o
Binary file not shown.
Binary file modified main.o
Binary file not shown.
16 changes: 14 additions & 2 deletions parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,19 @@ Node *add() {
Node *node = mul();
for (;;) {
if (consume("+")) {
node = new_node(ND_ADD, node, mul());
Node *r = mul();
if (node->type && node->type->ty == PTR) {
int c = node->type->ptr_to->ty == INT ? 4 : 8;
r = new_node(ND_MUL, r, new_node_num(c));
}
node = new_node(ND_ADD, node, r);
} else if (consume("-")) {
node = new_node(ND_SUB, node, mul());
Node *r = mul();
if (node->type && node->type->ty == PTR) {
int c = node->type->ptr_to->ty == INT ? 4 : 8;
r = new_node(ND_MUL, r, new_node_num(c));
}
node = new_node(ND_SUB, node, r);
} else {
return node;
}
Expand Down Expand Up @@ -365,6 +375,7 @@ Node *define_variable() {
}
lvar->type = type;
node->offset = lvar->offset;
node->type = type;
locals[cur_func] = lvar;

return node;
Expand All @@ -380,5 +391,6 @@ Node *variable(Token *tok) {
error("undefined variable: %s\n", name);
}
node->offset = lvar->offset;
node->type = lvar->type;
return node;
}
Binary file modified parser.o
Binary file not shown.
8 changes: 8 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ int main(){
}
"

assert 4 "int main() {
int *p;
alloc(&p, 1, 2, 4, 8);
int *q;
q = p + 2;
return *q;
}"


# define variables with type "int"
# assert 5 "int main() {
Expand Down
Binary file modified tokenize.o
Binary file not shown.

0 comments on commit f33a74f

Please sign in to comment.