-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3152217
commit 5cdf1c4
Showing
4 changed files
with
137 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#include "util.h" | ||
|
||
#include "../errors.h" | ||
|
||
void find_referenced_functions(function *f, function **functions, size_t *functions_size) { | ||
if (f->block == NULL) { | ||
// built-in | ||
return; | ||
} | ||
|
||
uint8_t *data = f->code.o; | ||
size_t size = f->code.size; | ||
|
||
size_t index = 0; | ||
while (index < size) { | ||
opcode *o = (opcode *)&data[index]; | ||
switch (o->type) { | ||
case OPCODE_CALL: { | ||
for (function_id i = 0; get_function(i) != NULL; ++i) { | ||
function *f = get_function(i); | ||
if (f->name == o->op_call.func) { | ||
if (f->block == NULL) { | ||
// built-in | ||
break; | ||
} | ||
|
||
bool found = false; | ||
for (size_t j = 0; j < *functions_size; ++j) { | ||
if (functions[j]->name == o->op_call.func) { | ||
found = true; | ||
break; | ||
} | ||
} | ||
if (!found) { | ||
functions[*functions_size] = f; | ||
*functions_size += 1; | ||
find_referenced_functions(f, functions, functions_size); | ||
} | ||
break; | ||
} | ||
} | ||
break; | ||
} | ||
} | ||
|
||
index += o->size; | ||
} | ||
} | ||
|
||
static void add_found_type(type_id t, type_id *types, size_t *types_size) { | ||
for (size_t i = 0; i < *types_size; ++i) { | ||
if (types[i] == t) { | ||
return; | ||
} | ||
} | ||
|
||
types[*types_size] = t; | ||
*types_size += 1; | ||
} | ||
|
||
void find_referenced_types(function *f, type_id *types, size_t *types_size) { | ||
if (f->block == NULL) { | ||
// built-in | ||
return; | ||
} | ||
|
||
function *functions[256]; | ||
size_t functions_size = 0; | ||
|
||
functions[functions_size] = f; | ||
functions_size += 1; | ||
|
||
find_referenced_functions(f, functions, &functions_size); | ||
|
||
for (size_t l = 0; l < functions_size; ++l) { | ||
function *func = functions[l]; | ||
debug_context context = {0}; | ||
check(func->parameter_type.type != NO_TYPE, context, "Parameter type missing"); | ||
add_found_type(func->parameter_type.type, types, types_size); | ||
check(func->return_type.type != NO_TYPE, context, "Return type missing"); | ||
add_found_type(func->return_type.type, types, types_size); | ||
|
||
uint8_t *data = functions[l]->code.o; | ||
size_t size = functions[l]->code.size; | ||
|
||
size_t index = 0; | ||
while (index < size) { | ||
opcode *o = (opcode *)&data[index]; | ||
switch (o->type) { | ||
case OPCODE_VAR: | ||
add_found_type(o->op_var.var.type.type, types, types_size); | ||
break; | ||
} | ||
|
||
index += o->size; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#pragma once | ||
|
||
#include "../functions.h" | ||
|
||
#include <stdint.h> | ||
|
||
void find_referenced_functions(function *f, function **functions, size_t *functions_size); | ||
void find_referenced_types(function *f, type_id *types, size_t *types_size); |