Skip to content

Commit

Permalink
fix(compiler);fix linker function address bug
Browse files Browse the repository at this point in the history
  • Loading branch information
limuy2022 committed Apr 22, 2024
1 parent 7586bfd commit 2dcf650
Show file tree
Hide file tree
Showing 13 changed files with 29 additions and 24 deletions.
1 change: 1 addition & 0 deletions examples/expected_result/control_flow.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
1
2
4
5
1 change: 0 additions & 1 deletion examples/expected_result/func_call.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
908977
987

17 changes: 1 addition & 16 deletions examples/expected_result/if.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1 @@
a:=90
if a==90 {
println("i equal to 90")
}
if a==89 {
println("i equal to 89")
}
if a < 89 {
println("i less than 90")
} else {
if a % 2 == 0 {
println("i is even")
} else {
println("i is odd")
}
}
i equal to 90
1 change: 1 addition & 0 deletions examples/expected_result/match.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
get 90
out of range
2 changes: 1 addition & 1 deletion examples/func_call.trc
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ t1(90, 89, 77)
a:=9
b:=8
c:=7
print("{}{}{}", a, b, c)
println("{}{}{}", a, b, c)
2 changes: 1 addition & 1 deletion examples/helloworld.trc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
print("Hello World")
print("Hello World\n")
2 changes: 1 addition & 1 deletion examples/kmp.trc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import "std.algo.string"
print("{}", string::kmp("ababa", "aba"))
println("{}", string::kmp("ababa", "aba"))
8 changes: 8 additions & 0 deletions src/compiler/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1305,11 +1305,18 @@ impl ModuleUnit {
fn lex_function(&mut self, funcid: usize, body: &FuncBodyTy) -> RuntimeResult<(usize, usize)> {
if !self.first_func {
// 如果不是第一个函数,在末尾加上结束主程序的指令
// println!("run here.Inst num is {}", self.staticdata.inst.len());
self.first_func = true;
self.add_bycode(Opcode::Stop, NO_ARG);
// println!(
// "run here.Inst num is {}.Op {}",
// self.staticdata.inst.len(),
// self.staticdata.get_next_opcode_id()
// );
self.staticdata.function_split = Some(self.staticdata.get_last_opcode_id());
}
let begin_inst_idx = self.staticdata.get_next_opcode_id();
// println!("start addr:{}", begin_inst_idx);
let func_obj = self
.self_scope
.borrow()
Expand Down Expand Up @@ -1361,6 +1368,7 @@ impl ModuleUnit {
swap(&mut tmp, &mut self.self_scope.borrow_mut().funcs_temp_store);
for i in tmp {
let (code_begin, var_mem_sz) = self.lex_function(i.0, &i.1)?;
// println!("{}", code_begin);
self.staticdata
.funcs_pos
.push(FuncStorage::new(code_begin, var_mem_sz))
Expand Down
3 changes: 2 additions & 1 deletion src/compiler/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pub fn link<'a>(data_iter: impl Iterator<Item = &'a mut StaticData>) -> StaticDa
if inst_ori_id == pos.func_addr {
function_expected_pos = iter_of_function.next();
let mut added = pos.clone();
added.func_addr = data.get_last_opcode_id() as usize;
// 该项指令还没有加入,所以是next
added.func_addr = data.get_next_opcode_id() as usize;
func_record_tmp.push(added);
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/tools/dis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn dis(opt: crate::compiler::CompileOption, rustcode: bool) -> anyhow::Resul
for i in &static_data.dll_module_should_loaded {
println!("{}", i);
}
println!();
println!("Inst:");
for i in static_data.inst.iter().enumerate() {
if rustcode {
if i.1.operand.1 == ARG_WRONG {
Expand Down Expand Up @@ -64,6 +64,12 @@ pub fn dis(opt: crate::compiler::CompileOption, rustcode: bool) -> anyhow::Resul
println!();
}
}

println!("Function Info:");
for i in &static_data.funcs_pos {
println!(
"Function address:{} | Function Var table size:{}",
i.func_addr, i.var_table_sz
);
}
Ok(())
}
2 changes: 2 additions & 0 deletions src/tvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ impl<'a> Vm<'a> {

#[inline]
fn run_opcode(&mut self, pc: &mut usize) -> Result<(), RuntimeError> {
// println!("run opcode:{}", self.static_data.inst[*pc].opcode);
match self.static_data.inst[*pc].opcode {
Opcode::Add => operator_opcode!(add, self),
Opcode::Sub => operator_opcode!(sub, self),
Expand Down Expand Up @@ -430,6 +431,7 @@ impl<'a> Vm<'a> {
}
Opcode::Jump => {
*pc = self.static_data.inst[*pc].operand.0 as usize;
// *pc += 1;
return Ok(());
}
Opcode::LoadChar => unsafe {
Expand Down
2 changes: 2 additions & 0 deletions stdlib/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ pub fn print(fmt_string: str) -> void {
.write_all((**iter.next().unwrap()).to_string().as_bytes())
.unwrap();
}
// print!("{}", unsafe { (**iter.next().unwrap()).to_string() });
if let Some(j) = output_iter.next() {
if j != '}' {
return Err(ErrorInfo::new(t!(UNCLOSED_FORMAT), t!(FORMAT_STR_ERROR)));
}
}
} else {
io::stdout().write_all(&[i as u8]).unwrap();
// print!("{}", i);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_all_examples.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn test_run_examples() {
ans_path.push("expected_result");
ans_path.push(path.file_name().expect("not file name"));
ans_path.set_extension("txt");
println!("{}", ans_path.display());
println!("checking {}", ans_path.display());
let expected_res = read_to_string(ans_path).unwrap();
let assert = cmd.arg("run").arg(path);
assert.assert().success().stdout(expected_res);
Expand Down

0 comments on commit 2dcf650

Please sign in to comment.