Implement enough asm generation to compile rule110
This commit is contained in:
parent
870dcb105d
commit
3d2cd64c6d
84
src/main.rs
84
src/main.rs
@ -361,6 +361,17 @@ fn generate_assembly_linux_x64_block(operations: &Vec<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Operation>, 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<Token>, intrinsics: &HashMap<&str, (Vec<Datat
|
||||
{
|
||||
data.push(default_val.clone());
|
||||
}
|
||||
arrays.push(Arr { name: name.clone(), datatype, length: size , data });
|
||||
arrays.push(Arr { name: sanitize_name(name.clone()), datatype, length: size , data });
|
||||
}
|
||||
}
|
||||
else
|
||||
@ -1245,6 +1308,11 @@ fn extract_arrays(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Datat
|
||||
return Ok(arrays);
|
||||
}
|
||||
|
||||
fn sanitize_name(name: String) -> String
|
||||
{
|
||||
return name.replace("-", "_").replace("+", "_");
|
||||
}
|
||||
|
||||
fn str_to_datatype(s: &str, line: i32, col: i32) -> Result<Datatype, String>
|
||||
{
|
||||
match s
|
||||
@ -1350,7 +1418,7 @@ fn extract_functions(tokens: &mut Vec<Token>, intrinsics: &HashMap<&str, (Vec<Da
|
||||
println!("outs: {:?}", outs);
|
||||
}
|
||||
let block = parse_block(&mut tokens_iter, intrinsics, debug)?;
|
||||
functions.push(Function {name: word.clone(), ins, outs, content: block});
|
||||
functions.push(Function {name: sanitize_name(word.clone()), ins, outs, content: block});
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -1418,7 +1486,7 @@ fn parse_until_delimiter(tokens_iter: &mut Peekable<std::slice::Iter<Token>>, 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<std::slice::Iter<Token>>, 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<Vec<Token>, 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<Vec<Token>, 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
1 1 <= boolToStr println
|
||||
1 1 == boolToStr println
|
Loading…
x
Reference in New Issue
Block a user