-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvm_value.c
203 lines (173 loc) · 5.4 KB
/
vm_value.c
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ast.h"
#include "vm_state.h"
#include "vm_value.h"
static LLVMTypeRef
get_llvm_type(int type_specifier)
{
switch (type_specifier) {
case TYPE_INT:
return LLVMInt32Type();
case TYPE_FLOAT:
return LLVMFloatType();
default:
fprintf(stderr, "Unknown type specifier: %d\n", type_specifier);
exit(EXIT_FAILURE);
}
return NULL;
}
struct vm_value *
vm_value_new(int type_specifier, const char *identifier)
{
struct vm_value *vmval;
vmval = calloc(1, sizeof(struct vm_value));
if (!vmval) {
fprintf(stderr, "Memory allocation request failed.\n");
exit(EXIT_FAILURE);
}
vmval->type_specifier = type_specifier;
vmval->identifier = identifier;
vmval->llvm_type = get_llvm_type(type_specifier);
return vmval;
}
struct vm_value *
vm_value_new_from_int_constant(int int_constant)
{
struct vm_value *vmval;
vmval = calloc(1, sizeof(struct vm_value));
if (!vmval) {
fprintf(stderr, "Memory allocation request failed.\n");
exit(EXIT_FAILURE);
}
vmval->type_specifier = TYPE_INT;
vmval->llvm_type = get_llvm_type(TYPE_INT);
vmval->llvm_value = LLVMConstInt(vmval->llvm_type, int_constant, 0);
return vmval;
}
struct vm_value *
vm_value_new_from_float_constant(float float_constant)
{
struct vm_value *vmval;
vmval = calloc(1, sizeof(struct vm_value));
if (!vmval) {
fprintf(stderr, "Memory allocation request failed.\n");
exit(EXIT_FAILURE);
}
vmval->type_specifier = TYPE_FLOAT;
vmval->llvm_type = get_llvm_type(TYPE_FLOAT);
vmval->llvm_value = LLVMConstReal(vmval->llvm_type, float_constant);
return vmval;
}
struct vm_value *
vm_value_dup(struct vm_value *vmval)
{
struct vm_value *ret;
ret = vm_value_new(vmval->type_specifier, vmval->identifier);
ret = memcpy(ret, vmval, sizeof(struct vm_value));
return ret;
}
LLVMValueRef
vm_value_alloca(struct vm_state *vm, struct vm_value *vmval)
{
const char *identifier = vmval->identifier;
if (identifier == NULL)
identifier = "";
vmval->llvm_value = LLVMBuildAlloca(vm->builder, vmval->llvm_type,
identifier);
return vmval->llvm_value;
}
struct vm_value *
vm_value_build_math_op(struct vm_state *vm, int operation,
struct vm_value *lhs,
struct vm_value *rhs)
{
struct vm_value *res;
LLVMOpcode opcode;
res = vm_value_new(lhs->type_specifier, "");
switch (operation) {
case AST_ADD:
opcode = (res->type_specifier == TYPE_INT) ? LLVMAdd : LLVMFAdd;
break;
case AST_SUB:
opcode = (res->type_specifier == TYPE_INT) ? LLVMSub : LLVMFSub;
break;
case AST_MUL:
opcode = (res->type_specifier == TYPE_INT) ? LLVMMul: LLVMFMul;
break;
case AST_DIV:
opcode = (res->type_specifier == TYPE_INT) ? LLVMSDiv: LLVMFDiv;
break;
default:
fprintf(stderr, "Unknown math operation: %d\n", operation);
exit(EXIT_FAILURE);
}
res->llvm_value = LLVMBuildBinOp(vm->builder, opcode, lhs->llvm_value,
rhs->llvm_value, "");
return res;
}
struct vm_value *
vm_value_build_cmp_op(struct vm_state *vm, int operation,
struct vm_value *lhs,
struct vm_value *rhs)
{
struct vm_value *res;
LLVMOpcode opcode;
LLVMIntPredicate int_predicate;
LLVMRealPredicate float_predicate;
res = vm_value_new(lhs->type_specifier, "");
opcode = (res->type_specifier == TYPE_INT) ? LLVMICmp : LLVMFCmp;
switch (operation) {
case AST_GT:
if (res->type_specifier == TYPE_INT)
int_predicate = LLVMIntSGT;
else
float_predicate = LLVMRealOGT;
break;
case AST_LT:
if (res->type_specifier == TYPE_INT)
int_predicate = LLVMIntSLT;
else
float_predicate = LLVMRealOLT;
break;
case AST_EQ:
if (res->type_specifier == TYPE_INT)
int_predicate = LLVMIntEQ;
else
float_predicate = LLVMRealOEQ;
break;
case AST_NE:
if (res->type_specifier == TYPE_INT)
int_predicate = LLVMIntNE;
else
float_predicate = LLVMRealONE;
break;
case AST_LE:
if (res->type_specifier == TYPE_INT)
int_predicate = LLVMIntSLE;
else
float_predicate = LLVMRealOLE;
break;
case AST_GE:
if (res->type_specifier == TYPE_INT)
int_predicate = LLVMIntSGE;
else
float_predicate = LLVMRealOGE;
break;
default:
fprintf(stderr, "Unknown comparison operation: %d\n", operation);
exit(EXIT_FAILURE);
}
if (opcode == LLVMICmp) {
res->llvm_value = LLVMBuildICmp(vm->builder, int_predicate,
lhs->llvm_value,
rhs->llvm_value, "");
} else {
res->llvm_value = LLVMBuildFCmp(vm->builder, float_predicate,
lhs->llvm_value,
rhs->llvm_value, "");
}
return res;
}