-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtype.c
More file actions
68 lines (62 loc) · 1.59 KB
/
Copy pathtype.c
File metadata and controls
68 lines (62 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "9cc.h"
#include "type.h"
#include <stdlib.h>
int get_size(Type *type) {
if(type->ty == INT) {
return INT_SIZE;
} else if(type->ty == CHAR) {
return CHAR_SIZE;
} else if (type->ty == PTR){
return PTR_SIZE;
} else if (type->ty == ARRAY) {
return type->array_size * get_size(type->ptr_to);
}
}
void set_type(Node *node) {
Node *n;
Type *type = calloc(1, sizeof(Type));
if(!node || node->type) {
return;
}
set_type(node->lhs);
set_type(node->rhs);
set_type(node->cond);
set_type(node->then);
set_type(node->els);
set_type(node->init);
set_type(node->inc);
for(n = node->body; n; n = n->next) {
set_type(n);
}
switch(node->kind) {
case ND_ADD:
case ND_SUB:
case ND_MUL:
case ND_DIV:
if(node->rhs->type->ty == PTR || node->rhs->type->ty == ARRAY) {
node->type = node->rhs->type;
} else {
node->type = node->lhs->type;
}
return;
case ND_RETURN:
node->type = node->lhs->type;
return;
case ND_EQ:
case ND_NE:
case ND_LT:
case ND_LE:
case ND_CALL: // とりあえず返り値はintと仮定
type->ty = INT;
node->type = type;
return;
case ND_ADDR:
type->ty = PTR;
type->ptr_to = node->lhs->type;
node->type = type;
return;
case ND_DEREF:
node->type = node->lhs->type->ptr_to;
return;
}
}