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

View File

@@ -10,10 +10,6 @@ UTF32_Result :: struct
out_str: U32 out_str: U32
advance: S64 advance: S64
error : S64 error : S64
UTF16_Result :: struct
out_str: [2]U16
len : S64
error : S64
utf8_to_utf32 :: (c: *U8, max_advance: S64): UTF32_Result utf8_to_utf32 :: (c: *U8, max_advance: S64): UTF32_Result
@@ -55,21 +51,23 @@ utf8_to_utf32 :: (c: *U8, max_advance: S64): UTF32_Result
return result return result
utf32_to_utf16 :: (codepoint: U32): UTF16_Result UTF16_Result :: struct
result: UTF16_Result out_str: [2]U16
len : S64
error : S64
utf32_to_utf16 :: (codepoint: U32): [2]U16, S64
str: [2]U16
len := 0
if codepoint < 0x10000 if codepoint < 0x10000
result.out_str[0] = codepoint->U16 str[0] = codepoint->U16
result.out_str[1] = 0 len = 1
result.len = 1
elif codepoint <= 0x10FFFF elif codepoint <= 0x10FFFF
code: U32 = (codepoint - 0x10000) code: U32 = (codepoint - 0x10000)
result.out_str[0] = (0xD800 | (code >> 10))->U16 str[0] = (0xD800 | (code >> 10))->U16
result.out_str[1] = (0xDC00 | (code & 0x3FF))->U16 str[1] = (0xDC00 | (code & 0x3FF))->U16
result.len = 2 len = 2
else
result.error = 1
return result return str, len
process_heap: HANDLE process_heap: HANDLE
allocate :: (size: U64): *void allocate :: (size: U64): *void
@@ -77,7 +75,7 @@ allocate :: (size: U64): *void
process_heap = GetProcessHeap() process_heap = GetProcessHeap()
return HeapAlloc(process_heap, 0, size) return HeapAlloc(process_heap, 0, size)
string_to_string16 :: (in: String): *U16, S64 string_to_string16 :: (in: String): String16
in_str := &in[0] in_str := &in[0]
// @Note(Krzosa): Should be more then enough space // @Note(Krzosa): Should be more then enough space
alloc_size := (length_of(in)*2)+1 alloc_size := (length_of(in)*2)+1
@@ -86,10 +84,10 @@ string_to_string16 :: (in: String): *U16, S64
decode := utf8_to_utf32(in_str + i, length_of(in) - i) decode := utf8_to_utf32(in_str + i, length_of(in) - i)
if !decode.error if !decode.error
i += decode.advance i += decode.advance
encode := utf32_to_utf16(decode.out_str) s16, s16_len := utf32_to_utf16(decode.out_str)
if !encode.error if s16_len != 0
for j := 0, j < encode.len, j++ for j := 0, j < s16_len, j++
result.str[result.len++] = encode.out_str[j] result.str[result.len++] = s16[j]
else else
result.str[result.len++] = question_mark16 result.str[result.len++] = question_mark16
break break
@@ -98,12 +96,12 @@ string_to_string16 :: (in: String): *U16, S64
break break
result.str[result.len] = 0 result.str[result.len] = 0
return result.str, result.len return result
test_unicode :: () test_unicode :: ()
string := " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..." string := " 豈 更 車 賈 滑 串 句 龜 龜 契 金 喇 奈 懶 癩 羅 蘿 螺 裸 邏 樂 洛 烙 珞 落 酪 駱 亂 卵 欄 爛 蘭 鸞 嵐 濫 藍 襤 拉 臘 蠟 廊 朗 浪 狼 郎 來 冷 勞 擄 櫓 爐 盧 老 蘆 虜 路 露 魯 鷺 碌 祿 綠 菉 錄 鹿 論 壟 弄 籠 聾 牢 磊 賂 雷 壘 屢 樓 淚 漏 累 縷 陋 勒 肋 凜 凌 稜 綾 菱 陵 讀 拏 樂 諾 丹 寧 怒 率 異 北 磻 便 復 不 泌 數 索 參 塞 省 葉 說 殺 辰 沈 拾 若 掠 略 亮 兩 凉 梁 糧 良 諒 量 勵 ..."
string_result, string_result_len := string_to_string16(string) string_result := string_to_string16(string)
// print(string_result) print(string_result)
result := utf8_to_utf32(&"A"[0], 1) result := utf8_to_utf32(&"A"[0], 1)
assert(result.out_str == 'A, "Invalid decode") // ' assert(result.out_str == 'A, "Invalid decode") // '
@@ -154,19 +152,19 @@ window_procedure :: (hwnd: HWND, msg: UINT, wparam: WPARAM, lparam: LPARAM): LRE
else;; return DefWindowProcW(hwnd, msg, wparam, lparam) else;; return DefWindowProcW(hwnd, msg, wparam, lparam)
WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int WinMain :: (hInstance: HINSTANCE, hPrevInstance: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: int): int
window_name, window_name_len := string_to_string16("Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ") window_name := string_to_string16("Have a wonderful day! 豈 更 車 賈 滑 串 句 龜 ")
w := WNDCLASSW{ w := WNDCLASSW{
lpfnWndProc = window_procedure, lpfnWndProc = window_procedure,
hInstance = hInstance, hInstance = hInstance,
lpszClassName = window_name, lpszClassName = window_name.str,
} }
assert(RegisterClassW(&w) != 0) assert(RegisterClassW(&w) != 0)
window := CreateWindowExW( window := CreateWindowExW(
dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0, dwExStyle = 0, hWndParent = 0, hMenu = 0, lpParam = 0,
X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = 1280, nHeight = 720, X = CW_USEDEFAULT, Y = CW_USEDEFAULT, nWidth = 1280, nHeight = 720,
lpClassName = window_name, lpClassName = window_name.str,
lpWindowName = window_name, lpWindowName = window_name.str,
dwStyle = WS_OVERLAPPEDWINDOW, dwStyle = WS_OVERLAPPEDWINDOW,
hInstance = hInstance hInstance = hInstance
) )