Skip to content

Commit

Permalink
make gcc happy
Browse files Browse the repository at this point in the history
  • Loading branch information
UltimatePea committed Oct 20, 2024
1 parent 1637be8 commit 316103d
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 81 deletions.
8 changes: 6 additions & 2 deletions py-interpreter/c_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ def ci(imm):
case N(NT_StringConst(val), []):
return [f"rax = static_string_to_yyvalue({python_string_to_c_string(val)});"]
case N(NT_TupleCons(), args):
return [f"rax = tuple_to_yyvalue({len(args)}, (yyvalue[])" + '{' + ', '.join([compile_immediate(params, locals, arg) for arg in args]) + "});"]
if len(args) == 0:
return [f"rax = tuple_to_yyvalue(0, NULL);"] # you can't have an array of size 0
else:
return [f"rax = tuple_to_yyvalue({len(args)}, (yyvalue[])" + '{' + ', '.join([compile_immediate(params, locals, arg) for arg in args]) + "});"]
case N(NT_TupleProj(idx), [arg]):
return [f"rax = yyvalue_to_tuple({ci(arg)})[{idx}];"]
case N(NT_Builtin(name), args):
Expand Down Expand Up @@ -271,7 +274,8 @@ def do_compile_funcs(func_dict):

def do_make_exec():
shutil.copyfile(pass_utils.get_artifact_path("output.c"), "./yybcvm/pygen/output.c")
cmd = "make -C ./yybcvm pygen PYGEN_EXEC_PATH=../"+pass_utils.get_artifact_path("output.exe")
# llvm -O3 doesn't terminate gcc is better
cmd = "make -C ./yybcvm pygen CC=gcc PYGEN_EXEC_PATH=../"+pass_utils.get_artifact_path("output.exe")
print(cmd)
os.system(cmd)
print("[OK] [DONE] RUN PROGRAM WITH THIS CMD:")
Expand Down
5 changes: 3 additions & 2 deletions yybcvm/native/commandline.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ yyvalue yyGetCommandLineProgramName(){
yyvalue yyGetCommandLineArgs(){

if(global_argc < 2){
yyvalue elems[] = {};
return array_to_iso_addr(0, elems);
// yyvalue elems[] = {};
// return array_to_iso_addr(0, elems);
return array_to_iso_addr(0, NULL);
}

yyvalue argPtrString[global_argc - 1];
Expand Down
26 changes: 25 additions & 1 deletion yybcvm/native/common_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,28 @@ void yy_gc_init();
extern bool during_gc;

void initialize_runtime_stack();
void yy_pre_function_call_gc();
void yy_pre_function_call_gc();


#ifndef ENUM_YYVALUE_TYPE
#define ENUM_YYVALUE_TYPE

typedef enum {
type_empty_value = 0,
type_tuple = 1, // must point to dyn heap
type_int = 2,
type_double = 3,
type_static_string = 4,
type_boolean = 5,
type_pointer_to_function = 6,
type_pointer_to_static_object = 7,
type_pointer_to_stack = 8,
type_pointer_transfer_address = 9,
type_heap_string = 10, // 0x0A
type_heap_string_header = 11, // 0x0B
type_runtime_generic_ptr = 12, // 0x0C
type_constructor_tuple = 13, // 0x0D
type_pointer_to_c_stack = 14, // 0x0E // used only in the runtime, should not appear at all on the stack
} YYValueType;

#endif
40 changes: 20 additions & 20 deletions yybcvm/native/debug_print.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
#include "common_include.h"


void yy_debug_log_print_args(uint64_t num, const char *func_name, ...) {
va_list args;
va_start(args, func_name);
fprintf(stderr, "%s called with ", func_name);
// void yy_debug_log_print_args(uint64_t num, const char *func_name, ...) {
// va_list args;
// va_start(args, func_name);
// fprintf(stderr, "%s called with ", func_name);

yyvalue arg;
arg = va_arg(args, yyvalue);
fprintf(stderr, "ret = ");
yy_print_yyvalue(arg, 0);
fprintf(stderr, ", ");
// yyvalue arg;
// arg = va_arg(args, yyvalue);
// fprintf(stderr, "ret = ");
// yy_print_yyvalue(arg, 0);
// fprintf(stderr, ", ");

for (int i = 1; i < num; i++) {
arg = va_arg(args, yyvalue);
fprintf(stderr, "arg %d = ", i);
yy_print_yyvalue(arg, 0);
if (i != num - 1) {
fprintf(stderr, ", ");
}
va_end(args);
}
// for (int i = 1; i < num; i++) {
// arg = va_arg(args, yyvalue);
// fprintf(stderr, "arg %d = ", i);
// yy_print_yyvalue(arg, 0);
// if (i != num - 1) {
// fprintf(stderr, ", ");
// }
// va_end(args);
// }

fprintf(stderr, "\n");
}
// fprintf(stderr, "\n");
// }
56 changes: 20 additions & 36 deletions yybcvm/native/marshall.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,6 @@
*
*/

const int type_empty_value = 0;
const int type_tuple = 1; // must point to dyn heap
const int type_int = 2;
const int type_double = 3;
const int type_static_string = 4;
const int type_boolean = 5;
const int type_pointer_to_function = 6;
const int type_pointer_to_static_object = 7;
const int type_pointer_to_stack = 8;
const int type_pointer_transfer_address = 9;
const int type_heap_string = 10; // 0x0A
const int type_heap_string_header = 11; // 0x0B
const int type_runtime_generic_ptr = 12; // 0x0C
const int type_constructor_tuple = 13; // 0x0D
const int type_pointer_to_c_stack = 14; // 0x0E // used only in the runtime, should not appear at all on the stack

static int offset_type = 64;
static int offset_subtype = 72;
static int offset_length = 80;
Expand Down Expand Up @@ -113,12 +97,12 @@ uint64_t string_buffer_length_to_block_length(uint64_t length) {

yyvalue* yyvalue_to_heap_pointer(yyvalue arg) {
assert(yyvalue_is_heap_pointer(arg));
return (yyvalue*)(arg);
return (yyvalue*)(uintptr_t)(arg);
}

yyvalue heap_pointer_to_yyvalue(uint64_t type, uint64_t subtype, uint64_t raw_length, yyvalue* ptr) {
assert(type == type_tuple || type == type_heap_string || type == type_constructor_tuple);
yyvalue ret = (yyvalue)ptr;
yyvalue ret = (yyvalue)(uintptr_t)ptr;
yyvalue_set_type(&ret, type);
yyvalue_set_subtype(&ret, subtype);
yyvalue_set_raw_length(&ret, raw_length);
Expand Down Expand Up @@ -184,7 +168,7 @@ char * yyvalue_to_string(yyvalue arg) {
switch (yyvalue_get_type(arg))
{
case type_static_string:
return (char *)arg;
return (char *)(uintptr_t)arg;
break;
case type_heap_string:
return yyvalue_to_heap_string_pointer(arg);
Expand Down Expand Up @@ -215,12 +199,12 @@ bool yyvalue_to_bool(yyvalue arg){

yyvalue* yyvalue_to_tuple(yyvalue arg){
assert(yyvalue_get_type(arg) == type_tuple);
return (yyvalue *)arg;
return (yyvalue *)(uintptr_t)arg;
}

yyvalue* yyvalue_to_constructor_tuple_tuple(yyvalue arg){
assert(yyvalue_get_type(arg) == type_constructor_tuple);
return (yyvalue *)arg;
return (yyvalue *)(uintptr_t)arg;
}

uint64_t yyvalue_to_tuple_length(yyvalue arg){
Expand All @@ -240,7 +224,7 @@ uint64_t yyvalue_to_constructor_tuple_length(yyvalue arg){

yyvalue* yyvalue_to_constructor_tuple(yyvalue arg){
assert(yyvalue_get_type(arg) == type_constructor_tuple);
return (yyvalue *)arg;
return (yyvalue *)(uintptr_t)arg;
}

uint64_t yyvalue_to_constructor_tuple_idx(yyvalue arg){
Expand All @@ -266,26 +250,26 @@ uint64_t yyvalue_to_heap_string_length(yyvalue arg){

yy_function_type yyvalue_to_funcptr(yyvalue arg){
assert(yyvalue_get_type(arg) == type_pointer_to_function);
return (yy_function_type)arg;
return (yy_function_type)(uintptr_t)arg;
}

yyvalue* yyvalue_to_staticptr(yyvalue arg){
assert(yyvalue_get_type(arg) == type_pointer_to_static_object);
return (yyvalue*)arg;
return (yyvalue*)(uintptr_t)arg;
}

yyvalue* yyvalue_to_stackptr(yyvalue arg){
assert(yyvalue_get_type(arg) == type_pointer_to_stack);
return (yyvalue*)arg;
return (yyvalue*)(uintptr_t)arg;
}

yyvalue* yyvalue_to_transfer_address(yyvalue arg){
assert(yyvalue_get_type(arg) == type_pointer_transfer_address);
return (yyvalue*)arg;
return (yyvalue*)(uintptr_t)arg;
}

void* yyvalue_to_generic_ptr(yyvalue arg) {
return (void*)arg;
return (void*)(uintptr_t)arg;
}


Expand All @@ -294,7 +278,7 @@ yyvalue unit_to_yyvalue(){
}

yyvalue static_string_to_yyvalue(const char * str){
yyvalue ret = (yyvalue)str;
yyvalue ret = (yyvalue)(uintptr_t)str;
yyvalue_set_type(&ret, type_static_string);
yyvalue_set_raw_length(&ret, strlen(str) + 1);
return ret;
Expand All @@ -308,7 +292,7 @@ yyvalue yy_gcAllocateStringBuffer(uint64_t byte_length) {
yyvalue_set_raw_length(&header, byte_length);
yyvalue_set_type(&header, type_heap_string_header);
buffer_ptr[0] = header;
yyvalue ret = (yyvalue)buffer_ptr;
yyvalue ret = (yyvalue)(uintptr_t)buffer_ptr;
yyvalue_set_type(&ret, type_heap_string);
yyvalue_set_raw_length(&ret, byte_length);
// fprintf(stderr, "Allocated String Buffer byte_length = " PRIu64 ", strlen = " PRIu64 ", yy_get_strlen = " PRIu64 "\n",
Expand Down Expand Up @@ -341,25 +325,25 @@ yyvalue double_to_yyvalue(double i){


yyvalue funcptr_to_yyvalue(yy_function_type func) {
yyvalue ret = (yyvalue)func;
yyvalue ret = (yyvalue)(uintptr_t)func;
yyvalue_set_type(&ret, type_pointer_to_function);
return ret;
}

yyvalue staticptr_to_yyvalue(yyvalue* static_ptr) {
yyvalue ret = (yyvalue)static_ptr;
yyvalue ret = (yyvalue)(uintptr_t)static_ptr;
yyvalue_set_type(&ret, type_pointer_to_static_object);
return ret;
}

yyvalue stackptr_to_yyvalue(yyvalue* stackptr) {
yyvalue ret = (yyvalue)stackptr;
yyvalue ret = (yyvalue)(uintptr_t)stackptr;
yyvalue_set_type(&ret, type_pointer_to_stack);
return ret;
}

yyvalue transfer_address_to_yyvalue(yyvalue* transfer_address) {
yyvalue ret = (yyvalue)transfer_address;
yyvalue ret = (yyvalue)(uintptr_t)transfer_address;
yyvalue_set_type(&ret, type_pointer_transfer_address);
return ret;
}
Expand All @@ -371,15 +355,15 @@ yyvalue bool_to_yyvalue(bool b){
}

yyvalue raw_tuple_to_yyvalue(uint64_t length, const yyvalue* elems){
yyvalue ret = (yyvalue)elems;
yyvalue ret = (yyvalue)(uintptr_t)elems;
yyvalue_set_type(&ret, type_tuple);
yyvalue_set_raw_length(&ret, length);
return ret;
}


yyvalue raw_constructor_tuple_to_yyvalue(uint64_t idx, uint64_t length, const yyvalue *elems){
yyvalue ret = (yyvalue)elems;
yyvalue ret = (yyvalue)(uintptr_t)elems;
yyvalue_set_type(&ret, type_constructor_tuple);
yyvalue_set_raw_length(&ret, idx);
yyvalue_set_subtype(&ret, length);
Expand All @@ -403,7 +387,7 @@ yyvalue constructor_tuple_to_yyvalue(uint64_t idx, uint64_t length, const yyvalu
}

yyvalue runtime_heap_pointer_to_yyvalue(yyvalue* ptr){
yyvalue ret = (yyvalue)ptr;
yyvalue ret = (yyvalue)(uintptr_t)ptr;
yyvalue_set_type(&ret, type_runtime_generic_ptr);
return ret;
}
Expand Down
14 changes: 0 additions & 14 deletions yybcvm/native/marshall.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,6 @@
#include <string.h>
#include "type_defs.h"

extern const int type_empty_value;
extern const int type_tuple;
extern const int type_int;
extern const int type_double;
extern const int type_heap_string;
extern const int type_static_string;
extern const int type_boolean;
extern const int type_pointer_to_function;
extern const int type_pointer_to_static_object;
extern const int type_pointer_to_stack;
extern const int type_pointer_transfer_address;
extern const int type_heap_string_header;


// type and length
uint64_t yyvalue_get_type(yyvalue arg);
void yyvalue_set_type(yyvalue *arg, uint64_t type);
Expand Down
15 changes: 9 additions & 6 deletions yybcvm/vm/yybc_vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ void load_bytecode(const char* filename) {

// Read header (64 bytes)
uint64_t num_external_calls, num_files, num_funcs, num_strings, string_offset;
fread(&num_external_calls, sizeof(uint64_t), 1, f);
fread(&num_files, sizeof(uint64_t), 1, f);
fread(&num_funcs, sizeof(uint64_t), 1, f);
fread(&num_strings, sizeof(uint64_t), 1, f);
fread(&string_offset, sizeof(uint64_t), 1, f);
size_t readBytes = 0;
readBytes += fread(&num_external_calls, sizeof(uint64_t), 1, f);
readBytes += fread(&num_files, sizeof(uint64_t), 1, f);
readBytes += fread(&num_funcs, sizeof(uint64_t), 1, f);
readBytes += fread(&num_strings, sizeof(uint64_t), 1, f);
readBytes += fread(&string_offset, sizeof(uint64_t), 1, f);
assert(readBytes == 5 * sizeof(uint64_t));

num_external_calls = swap_uint64(num_external_calls);
num_files = swap_uint64(num_files);
Expand All @@ -98,7 +100,8 @@ void load_bytecode(const char* filename) {
// Allocate bytecode data
long byte_code_size = fileSize - 64;
byte_code_data = (uint8_t*)malloc(byte_code_size);
fread(byte_code_data, sizeof(uint8_t), byte_code_size, f);
readBytes = fread(byte_code_data, sizeof(uint8_t), byte_code_size, f);
assert(readBytes == byte_code_size);

fclose(f);

Expand Down

0 comments on commit 316103d

Please sign in to comment.