Switched to using memory copy in order to implement multiple returns

This commit is contained in:
Krzosa Karol
2022-06-16 13:16:27 +02:00
parent 9c58d4f116
commit 66d7c8ca52
2 changed files with 57 additions and 44 deletions

View File

@@ -394,18 +394,29 @@ gen_ast(Ast *ast){
}
CASE(RETURN, Return){
gen("return ");
if(is_tuple(node->resolved_type)) {
gen("(");
gen_simple_decl(node->resolved_type);
gen("){");
Scratch scratch;
Intern_String var_name = pctx->intern(unique_name(scratch, node));
gen_simple_decl(node->resolved_type, var_name);
gen(";");
int i = 0;
For(node->expr){
genln("memcpy(&%Q.m%d, ", var_name, i);
if(!is_array(it->resolved_type)) gen("&");
gen("(");
gen_expr(it);
gen(")");
gen(", sizeof(%Q.m%d));", var_name, i++);
}
genln("return %Q;", var_name);
return;
}
For(node->expr){
gen_expr(it); // @todo multiple_returns
if(!node->expr.is_last(&it))
gen(", ");
}
if(is_tuple(node->resolved_type)) gen("}");
assert(node->expr.len <= 1);
gen("return ");
For(node->expr) gen_expr(it);
gen(";");
BREAK();
}
@@ -552,16 +563,16 @@ gen_ast(Ast *ast){
gen_ast(it);
Scratch scratch;
gen_simple_decl(node->resolved_type);
String var_name = unique_name(scratch, node);
gen("%Q", var_name);
Intern_String var_name = pctx->intern(unique_name(scratch, node));
gen_simple_decl(node->resolved_type, var_name);
gen(" = ");
gen_expr(node->expr);
gen(";");
int i = 0;
For(node->vars)
gen("%Q = %Q.m%d;", it->name, var_name, i++);
For(node->vars){
gen("memcpy((void *)&%Q, (void *)&%Q.m%d, sizeof(%Q));", it->name, var_name, i++, it->name);
}
BREAK();
}
@@ -718,6 +729,7 @@ end_compilation(){
gen(R"==(
#include <stdint.h>
#include <math.h>
#include <string.h>
typedef int8_t S8;
typedef int16_t S16;
typedef int32_t S32;
@@ -755,6 +767,7 @@ typedef struct String{
}
}
Scratch scratch;
for(S64 i = 0; i < pctx->type_map.cap; i++){
Map_Key_Value *it = pctx->type_map.data + i;
if(!it->occupied) continue;
@@ -776,8 +789,10 @@ typedef struct String{
global_indent++;
For(type->agg.members){
genln("");
gen_simple_decl(it.type);
gen(" m%llu;", type->agg.members.get_index(&it));
// @todo remove intern from gen
Intern_String name = pctx->intern(string_fmt(scratch, "m%llu", type->agg.members.get_index(&it)));
gen_simple_decl(it.type, name);
gen(";");
}
global_indent--;
genln("} Tuple%llu;", type->type_id);