From 3d2cd64c6da4d635c1f76a1234561afcb1535f3b Mon Sep 17 00:00:00 2001 From: 0x4261756D <–38735823+0x4261756D@users.noreply.github.com> Date: Tue, 3 Jan 2023 21:52:48 +0100 Subject: [PATCH] Implement enough asm generation to compile rule110 --- src/main.rs | 84 ++++++++++++++++++++++++++++++++++++++----- tests/comparisons.qbl | 6 ++-- 2 files changed, 80 insertions(+), 10 deletions(-) diff --git a/src/main.rs b/src/main.rs index efe5336..f30a8e4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -361,6 +361,17 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tinc r12\n"; data.code += "\tinc r13\n"; } + Operation::Swap(line, col) => + { + data.code += format!("\t;;swp {}:{}\n", line, col).as_str(); + data.code += "\tmov rax, [queue+8*r12]\n"; + data.code += "\tmov rbx, [queue+8*r12+8]\n"; + data.code += "\tadd r12, 2\n"; + data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; + data.code += "\tmov [queue+8*r13], rbx\n"; + data.code += "\tmov [queue+8*r13+8], rax\n"; + data.code += "\tadd r13, 2\n"; + } Operation::While(while_operations, line, col) => { data.code += format!("\t;;while {}:{}\n", line, col).as_str(); @@ -468,6 +479,28 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tmov [queue+8*r13], rax\n"; data.code += "\tinc r13\n"; } + "+" => + { + data.code += "\tmov qword rax, [queue+8*r12]\n"; + data.code += "\tinc r12\n"; + data.code += "\tmov qword rbx, [queue+8*r12]\n"; + data.code += "\tinc r12\n"; + data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; + data.code += "\tadd rax, rbx\n"; + data.code += "\tmov [queue+8*r13], rax\n"; + data.code += "\tinc r13\n"; + } + "*" => + { + data.code += "\tmov qword rax, [queue+8*r12]\n"; + data.code += "\tinc r12\n"; + data.code += "\tmov qword rbx, [queue+8*r12]\n"; + data.code += "\tinc r12\n"; + data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; + data.code += "\tmul rbx\n"; + data.code += "\tmov [queue+8*r13], rax\n"; + data.code += "\tinc r13\n"; + } ">" => { data.code += "\tmov rbx, 0\n"; @@ -516,6 +549,28 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; } + "==" => + { + data.code += "\tmov rbx, 0\n"; + data.code += "\tmov rcx, 1\n"; + data.code += "\tmov rax, [queue+8*r12]\n"; + data.code += "\tcmp qword rax, [queue+8*r12+8]\n"; + data.code += "\tcmove rbx, rcx\n"; + data.code += "\tadd r12, 2\n"; + data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; + data.code += "\tmov qword [queue+8*r13], rbx\n"; + data.code += "\tinc r13\n"; + } + "&&" => + { + data.code += "\tmov rax, [queue+8*r12]\n"; + data.code += "\tmov rbx, [queue+8*r12+8]\n"; + data.code += "\tand rax, rbx\n"; + data.code += "\tadd r12, 2\n"; + data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; + data.code += "\tmov [queue+8*r13], rax\n"; + data.code += "\tinc r13\n"; + } _ => todo!("intrinsic {} {}:{}", name, line, col) } } @@ -529,7 +584,8 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tmov rax, [queue+8*r12]\n"; data.code += "\tinc r12\n"; data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; - data.code += format!("\tmov qword [queue+8*r13], [arr_{}+rax]\n", name).as_str(); + data.code += format!("\tmov qword rbx, [arr_{}+8*rax]\n", name).as_str(); + data.code += "\tmov qword [queue+8*r13], rbx\n"; data.code += "\tinc r13\n"; } "write" => @@ -537,10 +593,17 @@ fn generate_assembly_linux_x64_block(operations: &Vec, functions: &Ve data.code += "\tmov rax, [queue+8*r12]\n"; data.code += "\tinc r12\n"; data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; - data.code += format!("\tmov qword [arr_{}+rax], [queue+8*r12]\n", name).as_str(); + data.code += "\tmov qword rbx, [queue+8*r12]\n"; + data.code += format!("\tmov qword [arr_{}+8*rax], rbx\n", name).as_str(); data.code += "\tinc r12\n"; data.code += ASSEMBLY_LINUX_X64_TRY_RESET_QUEUE; } + "length" => + { + let array = arrays.iter().find(|x| &x.name == name).unwrap(); + data.code += format!("\tmov qword [queue+8*r13], {}\n", array.length).as_str(); + data.code += "\tinc r13\n"; + } _ => todo!("apply {}", word) } } @@ -1217,7 +1280,7 @@ fn extract_arrays(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec, intrinsics: &HashMap<&str, (Vec String +{ + return name.replace("-", "_").replace("+", "_"); +} + fn str_to_datatype(s: &str, line: i32, col: i32) -> Result { match s @@ -1350,7 +1418,7 @@ fn extract_functions(tokens: &mut Vec, intrinsics: &HashMap<&str, (Vec>, in } Token::Apply(name, word, line, col) => { - operations.push(Operation::Apply(name.clone(), word.clone(), *line, *col)); + operations.push(Operation::Apply(sanitize_name(name.clone()), word.clone(), *line, *col)); } Token::Keyword(word, line, col) => { @@ -1490,7 +1558,7 @@ fn parse_until_delimiter(tokens_iter: &mut Peekable>, in } else { - operations.push(Operation::FunctionCall(word.clone(), *line, *col)); + operations.push(Operation::FunctionCall(sanitize_name(word.clone()), *line, *col)); } } } @@ -1594,7 +1662,7 @@ fn tokenize(text: &str) -> Result, String> } else { - tokens.push(Token::Apply(application_name.clone(), word.clone(), line, col)); + tokens.push(Token::Apply(sanitize_name(application_name.clone()), word.clone(), line, col)); application_name.clear(); } word.clear(); @@ -1639,7 +1707,7 @@ fn tokenize(text: &str) -> Result, String> } else { - tokens.push(Token::Apply(application_name.clone(), word.clone(), line, col)); + tokens.push(Token::Apply(sanitize_name(application_name.clone()), word.clone(), line, col)); } } } diff --git a/tests/comparisons.qbl b/tests/comparisons.qbl index 5388d4f..ebcd278 100644 --- a/tests/comparisons.qbl +++ b/tests/comparisons.qbl @@ -24,8 +24,10 @@ function bool => str boolToStr 1 0 < boolToStr println 1 0 >= boolToStr println 1 0 <= boolToStr println - +1 0 == boolToStr println +"" println 1 1 > boolToStr println 1 1 < boolToStr println 1 1 >= boolToStr println -1 1 <= boolToStr println \ No newline at end of file +1 1 <= boolToStr println +1 1 == boolToStr println \ No newline at end of file