Skip to content

Commit 1b8aba0

Browse files
print runtime errors working
Signed-off-by: salaheldinsoliman <[email protected]>
1 parent 06798cd commit 1b8aba0

File tree

4 files changed

+123
-3
lines changed

4 files changed

+123
-3
lines changed

src/codegen/dispatch/soroban.rs

+20
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,11 @@ pub fn function_dispatch(
102102

103103
wrapper_cfg.add(&mut vartab, placeholder);
104104

105+
<<<<<<< HEAD
106+
107+
if value.len() == 1 {
108+
=======
109+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
105110
// set the msb 8 bits of the return value to 6, the return value is 64 bits.
106111
// FIXME: this assumes that the solidity function always returns one value.
107112
let shifted = Expression::ShiftLeft {
@@ -131,6 +136,21 @@ pub fn function_dispatch(
131136
};
132137

133138
wrapper_cfg.add(&mut vartab, Instr::Return { value: vec![added] });
139+
<<<<<<< HEAD
140+
} else {
141+
142+
// return 2 as numberliteral
143+
let two = Expression::NumberLiteral {
144+
loc: pt::Loc::Codegen,
145+
ty: Type::Uint(64),
146+
value: BigInt::from(2_u64),
147+
};
148+
149+
wrapper_cfg.add(&mut vartab, Instr::Return { value: vec![two] });
150+
}
151+
152+
=======
153+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
134154

135155
vartab.finalize(ns, &mut wrapper_cfg);
136156
cfg.public = false;

src/emit/soroban/mod.rs

+19
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ use std::sync;
2222
const SOROBAN_ENV_INTERFACE_VERSION: u64 = 90194313216;
2323
pub const PUT_CONTRACT_DATA: &str = "l._";
2424
pub const GET_CONTRACT_DATA: &str = "l.1";
25+
<<<<<<< HEAD
26+
pub const LOG_FROM_LINEAR_MEMORY: &str = "x._";
27+
=======
28+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
2529

2630
pub struct SorobanTarget;
2731

@@ -231,12 +235,27 @@ impl SorobanTarget {
231235
.i64_type()
232236
.fn_type(&[ty.into(), ty.into()], false);
233237

238+
<<<<<<< HEAD
239+
let log_function_ty = binary
240+
.context
241+
.i64_type()
242+
.fn_type(&[ty.into(), ty.into(), ty.into(), ty.into()], false);
243+
244+
=======
245+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
234246
binary
235247
.module
236248
.add_function(PUT_CONTRACT_DATA, function_ty_1, Some(Linkage::External));
237249
binary
238250
.module
239251
.add_function(GET_CONTRACT_DATA, function_ty, Some(Linkage::External));
252+
<<<<<<< HEAD
253+
254+
binary
255+
.module
256+
.add_function(LOG_FROM_LINEAR_MEMORY, log_function_ty, Some(Linkage::External));
257+
=======
258+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
240259
}
241260

242261
fn emit_initializer(binary: &mut Binary, _ns: &ast::Namespace) {

src/emit/soroban/target.rs

+81
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22

33
use crate::codegen::cfg::HashTy;
44
use crate::codegen::Expression;
5+
<<<<<<< HEAD
6+
use crate::emit::binary::{self, Binary};
7+
use crate::emit::soroban::{SorobanTarget, GET_CONTRACT_DATA, LOG_FROM_LINEAR_MEMORY, PUT_CONTRACT_DATA};
8+
=======
59
use crate::emit::binary::Binary;
610
use crate::emit::soroban::{SorobanTarget, GET_CONTRACT_DATA, PUT_CONTRACT_DATA};
11+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
712
use crate::emit::ContractArgs;
813
use crate::emit::{TargetRuntime, Variable};
914
use crate::emit_context;
@@ -13,10 +18,17 @@ use crate::sema::ast::{Function, Namespace, Type};
1318

1419
use inkwell::types::{BasicTypeEnum, IntType};
1520
use inkwell::values::{
21+
<<<<<<< HEAD
22+
AnyValue, ArrayValue, AsValueRef, BasicMetadataValueEnum, BasicValue, BasicValueEnum, FunctionValue, IntValue, PointerValue
23+
};
24+
25+
use inkwell::AddressSpace;
26+
=======
1627
ArrayValue, BasicMetadataValueEnum, BasicValue, BasicValueEnum, FunctionValue, IntValue,
1728
PointerValue,
1829
};
1930

31+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
2032
use solang_parser::pt::Loc;
2133

2234
use std::collections::HashMap;
@@ -236,7 +248,76 @@ impl<'a> TargetRuntime<'a> for SorobanTarget {
236248

237249
/// Prints a string
238250
/// TODO: Implement this function, with a call to the `log` function in the Soroban runtime.
251+
<<<<<<< HEAD
252+
fn print(&self, bin: &Binary, string: PointerValue, length: IntValue) {
253+
254+
255+
256+
let mut toprint;
257+
unsafe {
258+
toprint = string.as_value_ref().offset(0);
259+
println!( "TO PRINT! {:?}", toprint);
260+
}
261+
//println!("print called with string: {:?} ", string.as_value_ref());
262+
println!("msg_pos: {:?}", string);
263+
println!("length: {:?}", length);
264+
265+
let msg_pos = bin.builder.build_ptr_to_int(string, bin.context.i64_type(), "msg_pos").unwrap();
266+
267+
let msg_pos = msg_pos.const_cast(bin.context.i64_type(), false);
268+
269+
270+
271+
println!("msg_pos extracted: {:?}", msg_pos);
272+
println!("=============================================================");
273+
274+
275+
276+
277+
//let length = bin.context.i64_type().const_int(1024, false);
278+
279+
let length = length.const_cast(bin.context.i64_type(), false);
280+
281+
282+
let eight = bin.context.i64_type().const_int(8, false);
283+
let four = bin.context.i64_type().const_int(4, false);
284+
let zero = bin.context.i64_type().const_int(0, false);
285+
let thirty_two = bin.context.i64_type().const_int(32, false);
286+
287+
// encode msg_pos and length
288+
let msg_pos_encoded = bin.builder.build_left_shift(msg_pos, thirty_two, "temp").unwrap();
289+
let msg_pos_encoded = bin.builder.build_int_add(msg_pos_encoded, four, "msg_pos_encoded").unwrap();
290+
291+
292+
293+
let length_encoded = bin.builder.build_left_shift(length, thirty_two, "temp").unwrap();
294+
let length_encoded = bin.builder.build_int_add(length_encoded, four, "length_encoded").unwrap();
295+
296+
297+
let zero_encoded = bin.builder.build_left_shift(zero, eight, "temp").unwrap();
298+
299+
300+
let eight_encoded = bin.builder.build_left_shift(eight, eight, "temp").unwrap();
301+
let eight_encoded = bin.builder.build_int_add(eight_encoded, four, "eight_encoded").unwrap();
302+
303+
304+
305+
306+
let call_res = bin.builder.build_call(
307+
bin.module.get_function(LOG_FROM_LINEAR_MEMORY).unwrap(),
308+
&[
309+
msg_pos_encoded.into(),
310+
length_encoded.into(),
311+
msg_pos_encoded.into(),
312+
four.into(),
313+
],
314+
"log",
315+
).unwrap();
316+
317+
}
318+
=======
239319
fn print(&self, bin: &Binary, string: PointerValue, length: IntValue) {}
320+
>>>>>>> 06798cdeac6fd62ee98f5ae7da38f3af4933dc0f
240321

241322
/// Return success without any result
242323
fn return_empty_abi(&self, bin: &Binary) {

src/linker/soroban_wasm.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ use wasm_encoder::{
1111
};
1212
use wasmparser::{Global, Import, Parser, Payload::*, SectionLimited, TypeRef};
1313

14-
use crate::emit::soroban::GET_CONTRACT_DATA;
15-
use crate::emit::soroban::PUT_CONTRACT_DATA;
14+
use crate::emit::soroban::{GET_CONTRACT_DATA, PUT_CONTRACT_DATA, LOG_FROM_LINEAR_MEMORY};
1615

1716
pub fn link(input: &[u8], name: &str) -> Vec<u8> {
1817
let dir = tempdir().expect("failed to create temp directory for linking");
@@ -82,7 +81,7 @@ fn generate_module(input: &[u8]) -> Vec<u8> {
8281
module.finish()
8382
}
8483

85-
/// Resolve all pallet contracts runtime imports
84+
/// Resolve all soroban contracts runtime imports
8685
fn generate_import_section(section: SectionLimited<Import>, module: &mut Module) {
8786
let mut imports = ImportSection::new();
8887
for import in section.into_iter().map(|import| import.unwrap()) {
@@ -98,6 +97,7 @@ fn generate_import_section(section: SectionLimited<Import>, module: &mut Module)
9897
};
9998
let module_name = match import.name {
10099
GET_CONTRACT_DATA | PUT_CONTRACT_DATA => "l",
100+
LOG_FROM_LINEAR_MEMORY => "x",
101101
_ => panic!("got func {:?}", import),
102102
};
103103
// parse the import name to all string after the the first dot

0 commit comments

Comments
 (0)