Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
rpkak committed Dec 9, 2023
1 parent 77449a7 commit 17d558d
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 77 deletions.
125 changes: 125 additions & 0 deletions src/ptlang_ast/ptlang_ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,9 @@ void ptlang_ast_exp_destroy(ptlang_ast_exp exp)
case PTLANG_AST_EXP_REFERENCE:
ptlang_ast_exp_destroy(exp->content.reference.value);
break;
case PTLANG_AST_EXP_BINARY:
ptlang_free(exp->content.binary);
break;
}

ptlang_free(exp);
Expand Down Expand Up @@ -994,4 +997,126 @@ ptlang_ast_ident ptlang_ast_ident_copy(ptlang_ast_ident ident)
};
memcpy(new_ident.name, ident.name, name_len);
return new_ident;
}

ptlang_ast_exp ptlang_ast_exp_copy(ptlang_ast_exp exp)
{
ptlang_ast_exp copy = ptlang_malloc(sizeof(struct ptlang_ast_exp_s));
copy->ast_type = ptlang_ast_type_copy(exp->ast_type);
copy->type = exp->type;
copy->pos = ptlang_ast_code_position_copy(exp->pos);
switch (exp->type)
{
case PTLANG_AST_EXP_ASSIGNMENT:
case PTLANG_AST_EXP_ADDITION:
case PTLANG_AST_EXP_SUBTRACTION:
case PTLANG_AST_EXP_MULTIPLICATION:
case PTLANG_AST_EXP_DIVISION:
case PTLANG_AST_EXP_MODULO:
case PTLANG_AST_EXP_REMAINDER:
case PTLANG_AST_EXP_EQUAL:
case PTLANG_AST_EXP_NOT_EQUAL:
case PTLANG_AST_EXP_GREATER:
case PTLANG_AST_EXP_GREATER_EQUAL:
case PTLANG_AST_EXP_LESS:
case PTLANG_AST_EXP_LESS_EQUAL:
case PTLANG_AST_EXP_LEFT_SHIFT:
case PTLANG_AST_EXP_RIGHT_SHIFT:
case PTLANG_AST_EXP_AND:
case PTLANG_AST_EXP_OR:
case PTLANG_AST_EXP_BITWISE_AND:
case PTLANG_AST_EXP_BITWISE_OR:
case PTLANG_AST_EXP_BITWISE_XOR:
{
copy->content.binary_operator.left_value =
ptlang_ast_exp_copy(exp->content.binary_operator.left_value);
copy->content.binary_operator.right_value =
ptlang_ast_exp_copy(exp->content.binary_operator.right_value);
break;
}
case PTLANG_AST_EXP_NEGATION:
case PTLANG_AST_EXP_NOT:
case PTLANG_AST_EXP_BITWISE_INVERSE:
case PTLANG_AST_EXP_LENGTH:
case PTLANG_AST_EXP_DEREFERENCE:
{
copy->content.unary_operator = ptlang_ast_exp_copy(exp->content.unary_operator);
break;
}
case PTLANG_AST_EXP_FUNCTION_CALL:
{
copy->content.function_call.function = ptlang_ast_exp_copy(copy->content.function_call.function);
copy->content.function_call.parameters = NULL;
for (size_t i = 0; i < arrlenu(exp->content.function_call.parameters); i++)
{
arrpush(copy->content.function_call.parameters,
ptlang_ast_exp_copy(exp->content.function_call.parameters[i]));
}
break;
}
case PTLANG_AST_EXP_VARIABLE:
case PTLANG_AST_EXP_INTEGER:
case PTLANG_AST_EXP_FLOAT:
{
size_t str_size = strlen(exp->content.str_prepresentation) + 1;
copy->content.str_prepresentation = ptlang_malloc(str_size);
memcpy(copy->content.str_prepresentation, exp->content.str_prepresentation, str_size);
break;
}
case PTLANG_AST_EXP_STRUCT:
{
copy->content.struct_.type = ptlang_ast_ident_copy(exp->content.struct_.type);
for (size_t i = 0; i < arrlenu(exp->content.struct_.members); i++)
{
arrpush(copy->content.struct_.members,
((struct ptlang_ast_struct_member_s){
.exp = ptlang_ast_exp_copy(exp->content.struct_.members[i].exp),
.pos = ptlang_ast_code_position_copy(exp->content.struct_.members[i].pos),
.str = ptlang_ast_ident_copy(exp->content.struct_.members[i].str)}));
}
break;
}
case PTLANG_AST_EXP_ARRAY:
{
copy->content.array.type = ptlang_ast_type_copy(exp->content.array.type);
for (size_t i = 0; i < arrlenu(exp->content.array.values); i++)
{
arrpush(copy->content.array.values, ptlang_ast_exp_copy(exp->content.array.values[i]));
}
break;
}
case PTLANG_AST_EXP_TERNARY:
copy->content.ternary_operator.condition =
ptlang_ast_exp_copy(copy->content.ternary_operator.condition);
copy->content.ternary_operator.if_value = ptlang_ast_exp_copy(exp->content.ternary_operator.if_value);
copy->content.ternary_operator.else_value =
ptlang_ast_exp_copy(exp->content.ternary_operator.else_value);
break;
case PTLANG_AST_EXP_CAST:
copy->content.cast.type = ptlang_ast_type_copy(exp->content.cast.type);
copy->content.cast.value = ptlang_ast_exp_copy(exp->content.cast.value);
break;
case PTLANG_AST_EXP_STRUCT_MEMBER:
copy->content.struct_member.struct_ = ptlang_ast_exp_copy(exp->content.struct_member.struct_);
copy->content.struct_member.member_name =
ptlang_ast_ident_copy(exp->content.struct_member.member_name);
break;
case PTLANG_AST_EXP_ARRAY_ELEMENT:
copy->content.array_element.array = ptlang_ast_exp_copy(exp->content.array_element.array);
copy->content.array_element.index = ptlang_ast_exp_copy(exp->content.array_element.index);
break;
case PTLANG_AST_EXP_REFERENCE:
copy->content.reference.value = ptlang_ast_exp_copy(exp->content.reference.value);
copy->content.reference.writable = exp->content.reference.writable;
break;
case PTLANG_AST_EXP_BINARY:
{
uint32_t bit_size = exp->ast_type->type == PTLANG_AST_TYPE_INTEGER
? exp->ast_type->content.integer.size
: exp->ast_type->content.float_size;
uint32_t byte_size = (bit_size - 1) / 8 + 1;
memcpy(copy->content.binary, exp->content.binary, byte_size);
}
}
return copy;
}
2 changes: 2 additions & 0 deletions src/ptlang_ast/ptlang_ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,7 @@ ptlang_ast_decl ptlang_decl_list_find_last(ptlang_ast_decl *decl_list, char *nam

ptlang_ast_code_position ptlang_ast_code_position_copy(ptlang_ast_code_position pos);

ptlang_ast_exp ptlang_ast_exp_copy(ptlang_ast_exp exp);


#endif
18 changes: 4 additions & 14 deletions src/ptlang_eval/ptlang_eval.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,8 @@ ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp)

LLVMValueRef function = LLVMAddFunction(M, "main", func_type);

LLVMTypeRef size_func_type = LLVMFunctionType(LLVMInt64TypeInContext(C), NULL, 0, false);

LLVMValueRef size_func = LLVMAddFunction(M, "size", size_func_type);

LLVMBuilderRef B = LLVMCreateBuilderInContext(C);

LLVMBasicBlockRef size_entry = LLVMAppendBasicBlockInContext(C, size_func, "size_entry");

LLVMPositionBuilderAtEnd(B, size_entry);
LLVMValueRef size_value = LLVMSizeOf(type);
LLVMBuildRet(B, size_value);

LLVMBasicBlockRef entry = LLVMAppendBasicBlockInContext(C, function, "entry");

ptlang_ir_builder_build_context cxt = {
Expand All @@ -41,12 +31,12 @@ ptlang_ast_exp ptlang_eval_const_exp(ptlang_ast_exp exp)
LLVMExecutionEngineRef ee;
LLVMCreateExecutionEngineForModule(&ee, M, NULL);

LLVMGenericValueRef in_llvm_size = LLVMRunFunction(ee, size_func, 0, NULL);
unsigned long long size = LLVMGenericValueToInt(in_llvm_size, false);
uint32_t bit_size = exp->ast_type->type == PTLANG_AST_TYPE_INTEGER ? exp->ast_type->content.integer.size
: exp->ast_type->content.float_size;

uint8_t *binary = NULL;
uint8_t *binary = ptlang_malloc((bit_size - 1) / 8 + 1);

arrsetlen(binary, size);
// arrsetlen(binary, size);

LLVMGenericValueRef in_llvm_binary = LLVMCreateGenericValueOfPointer(binary);
LLVMRunFunction(ee, function, 1, &in_llvm_binary);
Expand Down
24 changes: 22 additions & 2 deletions src/ptlang_ir_builder/ptlang_ir_builder.c
Original file line number Diff line number Diff line change
Expand Up @@ -1677,9 +1677,29 @@ LLVMValueRef ptlang_ir_builder_exp(ptlang_ast_exp exp, ptlang_ir_builder_build_c
}
case PTLANG_AST_EXP_BINARY:
{
size_t bytes = arrlenu(exp->content.binary);

// TODO create array of LLVMValueRefs of bytes, bytecast to LLVMValueRef of ast_type
uint32_t bit_size = exp->ast_type->type == PTLANG_AST_TYPE_INTEGER
? exp->ast_type->content.integer.size
: exp->ast_type->content.float_size;
uint32_t byte_size = (bit_size - 1) / 8 + 1;

LLVMValueRef *bytes = ptlang_malloc(sizeof(LLVMValueRef) * byte_size);

LLVMTypeRef byte = LLVMInt8Type();

for (uint32_t i = 0; i < byte_size; i++)
{
bytes[i] = LLVMConstInt(byte, exp->content.binary[i], false);
}

LLVMValueRef as_array = LLVMConstArray(LLVMArrayType(byte, byte_size), bytes, byte_size);

LLVMValueRef as_int = LLVMConstBitCast(as_array, LLVMIntType(byte_size * 8));
if (bit_size != byte_size * 8)
{
as_int = LLVMConstTrunc(as_int, LLVMIntType(bit_size));
}
return as_int;
}
}
abort();
Expand Down
24 changes: 21 additions & 3 deletions src/ptlang_main/ptlang_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
ptlang_context_type_to_string(type->content.function.return_type, NULL, type_scope);
for (size_t i = 0; i < arrlenu(type->content.function.parameters); i++)
{
size +=
ptlang_context_type_to_string(type->content.function.parameters[i], NULL, type_scope) - 1 + sizeof(", ") - 1;
size += ptlang_context_type_to_string(type->content.function.parameters[i], NULL, type_scope) -
1 + sizeof(", ") - 1;
}
if (arrlenu(type->content.function.parameters) != 0)
{
Expand All @@ -134,7 +134,8 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
out++;
for (size_t i = 0; i < arrlenu(type->content.function.parameters); i++)
{
out += ptlang_context_type_to_string(type->content.function.parameters[i], out, type_scope) - 1;
out +=
ptlang_context_type_to_string(type->content.function.parameters[i], out, type_scope) - 1;
if (i < arrlenu(type->content.function.parameters) - 1)
{
*out = ',';
Expand Down Expand Up @@ -218,3 +219,20 @@ size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_con
}
return size;
}

ptlang_ast_struct_def ptlang_context_get_struct_def(char *name, ptlang_context_type_scope *type_scope)
{

ptlang_ast_struct_def struct_def = NULL;
while (true)
{
ptlang_context_type_scope_entry entry = shget(type_scope, name);
if (entry.type == PTLANG_CONTEXT_TYPE_SCOPE_ENTRY_STRUCT)
{
struct_def = entry.value.struct_def;
break;
}
name = entry.value.ptlang_type->content.name;
}
return struct_def;
}
2 changes: 2 additions & 0 deletions src/ptlang_main/ptlang_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ bool ptlang_context_type_equals(ptlang_ast_type type_1, ptlang_ast_type type_2,

size_t ptlang_context_type_to_string(ptlang_ast_type type, char *out, ptlang_context_type_scope *type_scope);

ptlang_ast_struct_def ptlang_context_get_struct_def(char *name, ptlang_context_type_scope *type_scope);

#endif
6 changes: 3 additions & 3 deletions src/ptlang_utils/ptlang_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <stdbool.h>
#include <stdlib.h>

#define ptlang_malloc(size) size == 0 ? NULL : malloc(size)
#define ptlang_malloc_zero(size) size == 0 ? NULL : memset(ptlang_malloc(size), 0, size)
#define ptlang_malloc(size) (size) == 0 ? NULL : malloc(size)
#define ptlang_malloc_zero(size) (size) == 0 ? NULL : memset(ptlang_malloc(size), 0, (size))
#define ptlang_free(ptr) free(ptr)
#define ptlang_realloc(ptr, size) size == 0 ? (free(ptr), NULL) : realloc(ptr, size)
#define ptlang_realloc(ptr, size) (size) == 0 ? (free(ptr), NULL) : realloc(ptr, (size))

#ifdef NDEBUG
# define ptlang_assert(val) ((void)val)
Expand Down
Loading

0 comments on commit 17d558d

Please sign in to comment.