2
2
3
3
use crate :: codegen:: cfg:: HashTy ;
4
4
use crate :: codegen:: Expression ;
5
- use crate :: emit:: binary:: Binary ;
6
- use crate :: emit:: soroban:: { SorobanTarget , GET_CONTRACT_DATA , PUT_CONTRACT_DATA } ;
5
+ use crate :: emit:: binary:: { self , Binary } ;
6
+ use crate :: emit:: soroban:: {
7
+ SorobanTarget , GET_CONTRACT_DATA , LOG_FROM_LINEAR_MEMORY , PUT_CONTRACT_DATA ,
8
+ } ;
7
9
use crate :: emit:: ContractArgs ;
8
10
use crate :: emit:: { TargetRuntime , Variable } ;
9
11
use crate :: emit_context;
@@ -13,10 +15,11 @@ use crate::sema::ast::{Function, Namespace, Type};
13
15
14
16
use inkwell:: types:: { BasicTypeEnum , IntType } ;
15
17
use inkwell:: values:: {
16
- ArrayValue , BasicMetadataValueEnum , BasicValue , BasicValueEnum , FunctionValue , IntValue ,
17
- PointerValue ,
18
+ AnyValue , ArrayValue , AsValueRef , BasicMetadataValueEnum , BasicValue , BasicValueEnum ,
19
+ FunctionValue , IntValue , PointerValue ,
18
20
} ;
19
21
22
+ use inkwell:: AddressSpace ;
20
23
use solang_parser:: pt:: Loc ;
21
24
22
25
use std:: collections:: HashMap ;
@@ -236,7 +239,138 @@ impl<'a> TargetRuntime<'a> for SorobanTarget {
236
239
237
240
/// Prints a string
238
241
/// TODO: Implement this function, with a call to the `log` function in the Soroban runtime.
239
- fn print ( & self , bin : & Binary , string : PointerValue , length : IntValue ) { }
242
+ fn print ( & self , bin : & Binary , string : PointerValue , length : IntValue ) {
243
+ if string. is_const ( ) && length. is_const ( ) {
244
+ println ! ( "msg_pos: {:?}" , string) ;
245
+ println ! ( "length: {:?}" , length) ;
246
+
247
+ let msg_pos = bin
248
+ . builder
249
+ . build_ptr_to_int ( string, bin. context . i64_type ( ) , "msg_pos" )
250
+ . unwrap ( ) ;
251
+ let msg_pos = msg_pos. const_cast ( bin. context . i64_type ( ) , false ) ;
252
+
253
+ println ! ( "msg_pos extracted: {:?}" , msg_pos) ;
254
+ println ! ( "=============================================================" ) ;
255
+
256
+ let length = length. const_cast ( bin. context . i64_type ( ) , false ) ;
257
+
258
+ let eight = bin. context . i64_type ( ) . const_int ( 8 , false ) ;
259
+ let four = bin. context . i64_type ( ) . const_int ( 4 , false ) ;
260
+ let zero = bin. context . i64_type ( ) . const_int ( 0 , false ) ;
261
+ let thirty_two = bin. context . i64_type ( ) . const_int ( 32 , false ) ;
262
+
263
+ // encode msg_pos and length
264
+ let msg_pos_encoded = bin
265
+ . builder
266
+ . build_left_shift ( msg_pos, thirty_two, "temp" )
267
+ . unwrap ( ) ;
268
+ let msg_pos_encoded = bin
269
+ . builder
270
+ . build_int_add ( msg_pos_encoded, four, "msg_pos_encoded" )
271
+ . unwrap ( ) ;
272
+
273
+ let length_encoded = bin
274
+ . builder
275
+ . build_left_shift ( length, thirty_two, "temp" )
276
+ . unwrap ( ) ;
277
+ let length_encoded = bin
278
+ . builder
279
+ . build_int_add ( length_encoded, four, "length_encoded" )
280
+ . unwrap ( ) ;
281
+
282
+ let zero_encoded = bin. builder . build_left_shift ( zero, eight, "temp" ) . unwrap ( ) ;
283
+
284
+ let eight_encoded = bin. builder . build_left_shift ( eight, eight, "temp" ) . unwrap ( ) ;
285
+ let eight_encoded = bin
286
+ . builder
287
+ . build_int_add ( eight_encoded, four, "eight_encoded" )
288
+ . unwrap ( ) ;
289
+
290
+ let call_res = bin
291
+ . builder
292
+ . build_call (
293
+ bin. module . get_function ( LOG_FROM_LINEAR_MEMORY ) . unwrap ( ) ,
294
+ & [
295
+ msg_pos_encoded. into ( ) ,
296
+ length_encoded. into ( ) ,
297
+ msg_pos_encoded. into ( ) ,
298
+ four. into ( ) ,
299
+ ] ,
300
+ "log" ,
301
+ )
302
+ . unwrap ( ) ;
303
+ } else {
304
+ println ! ( "in else: " ) ;
305
+
306
+ println ! ( "msg_pos: {:?}" , string) ;
307
+ println ! ( "length: {:?}" , length) ;
308
+
309
+
310
+ /*println!("msg_pos: {:?}", string);
311
+ println!("length: {:?}", length);
312
+
313
+ let msg_pos = bin.builder.build_ptr_to_int(string, bin.context.i64_type(), "msg_pos").unwrap();
314
+ //let msg_pos = msg_pos.const_cast(bin.context.i64_type(), false);
315
+
316
+
317
+
318
+ println!("msg_pos extracted: {:?}", msg_pos);
319
+ println!("=============================================================");
320
+
321
+ //let length = length.const_cast(bin.context.i64_type(), false);
322
+
323
+
324
+ let eight = bin.context.i64_type().const_int(8, false);
325
+ let four = bin.context.i64_type().const_int(4, false);
326
+ let zero = bin.context.i64_type().const_int(0, false);
327
+ let thirty_two = bin.context.i64_type().const_int(32, false);
328
+
329
+ // encode msg_pos and length
330
+ let msg_pos_encoded = bin.builder.build_left_shift(msg_pos, thirty_two, "temp").unwrap();
331
+ let msg_pos_encoded = bin.builder.build_int_add(msg_pos_encoded, four, "msg_pos_encoded").unwrap();
332
+
333
+ println!("CAN MSG ENCODE");
334
+
335
+
336
+
337
+ //let length = bin.builder.build_int_z_extend(length, bin.context.i64_type(), "extended").unwrap();
338
+
339
+
340
+ let length_type = length.get_type();
341
+ println!("LENGTH TYPE: {:?}", length_type);
342
+
343
+ //let length = bin.builder.build_int_z_extend(length, bin.context.i64_type(), "extended").unwrap();
344
+ //let length = bin.builder.build_int_cast(length, bin.context.i64_type(), "extended").unwrap();
345
+
346
+ let length_encoded = bin.builder.build_left_shift(length, thirty_two, "temp").unwrap();
347
+ let length_encoded = bin.builder.build_int_add(length_encoded, four, "length_encoded").unwrap();
348
+
349
+ println!("CAN LENGTH ENCODE");
350
+
351
+
352
+ let zero_encoded = bin.builder.build_left_shift(zero, eight, "temp").unwrap();
353
+
354
+
355
+ let eight_encoded = bin.builder.build_left_shift(eight, eight, "temp").unwrap();
356
+ let eight_encoded = bin.builder.build_int_add(eight_encoded, four, "eight_encoded").unwrap();
357
+
358
+
359
+
360
+
361
+ let call_res = bin.builder.build_call(
362
+ bin.module.get_function(LOG_FROM_LINEAR_MEMORY).unwrap(),
363
+ &[
364
+ msg_pos_encoded.into(),
365
+ length_encoded.into(),
366
+ msg_pos_encoded.into(),
367
+ four.into(),
368
+ ],
369
+ "log",
370
+ ).unwrap();
371
+ */
372
+ }
373
+ }
240
374
241
375
/// Return success without any result
242
376
fn return_empty_abi ( & self , bin : & Binary ) {
0 commit comments