Distinguish between global/local pages

Fix bug when calling variable_type on global variable (e.g. in A_ALLOC
on global).
This commit is contained in:
Nunuhara Cabbage
2019-11-08 22:02:47 -08:00
parent 1e3fbcd95f
commit ec9ab8954c
3 changed files with 7 additions and 3 deletions
+5
View File
@@ -24,6 +24,7 @@
#define CACHE_SIZE 64
static const char *pagetype_strtab[] = {
[GLOBAL_PAGE] = "GLOBAL_PAGE",
[LOCAL_PAGE] = "LOCAL_PAGE",
[STRUCT_PAGE] = "STRUCT_PAGE",
[ARRAY_PAGE] = "ARRAY_PAGE"
@@ -123,6 +124,10 @@ enum ain_data_type array_type(enum ain_data_type type)
enum ain_data_type variable_type(struct page *page, int varno, enum ain_data_type *struct_type)
{
switch (page->type) {
case GLOBAL_PAGE:
if (struct_type)
*struct_type = ain->globals[varno].struct_type;
return ain->globals[varno].data_type;
case LOCAL_PAGE:
if (struct_type)
*struct_type = ain->functions[page->index].vars[varno].struct_type;
+1
View File
@@ -25,6 +25,7 @@ struct ain_variable;
enum ain_data_type;
enum page_type {
GLOBAL_PAGE,
LOCAL_PAGE,
STRUCT_PAGE,
ARRAY_PAGE
+1 -3
View File
@@ -1127,9 +1127,7 @@ void vm_execute_ain(struct ain *program)
link_libraries();
// Initialize globals
heap[0].page = xmalloc(sizeof(struct page) + sizeof(union vm_value) * ain->nr_globals);
heap[0].page->nr_vars = ain->nr_globals;
//heap[0].page->vars = ain->globals; // FIXME: wrong type
heap[0].page = alloc_page(GLOBAL_PAGE, 0, ain->nr_globals);
for (int i = 0; i < ain->nr_globals; i++) {
switch (ain->globals[i].data_type) {
case AIN_STRING: