1
1
// Copyright (c) 2020 Ghaith Hachem and Mathias Rieder
2
2
use super :: {
3
- expression_generator:: { to_i1, ExpressionCodeGenerator } ,
3
+ expression_generator:: { to_i1, ExpressionCodeGenerator , ExpressionValue } ,
4
4
llvm:: Llvm ,
5
5
} ;
6
6
use crate :: {
@@ -11,7 +11,7 @@ use crate::{
11
11
} ,
12
12
index:: { ImplementationIndexEntry , Index } ,
13
13
resolver:: { AnnotationMap , AstAnnotations , StatementAnnotation } ,
14
- typesystem:: { get_bigger_type, DataTypeInformation } ,
14
+ typesystem:: { get_bigger_type, DataTypeInformation , DINT_TYPE } ,
15
15
} ;
16
16
use inkwell:: {
17
17
basic_block:: BasicBlock ,
@@ -332,8 +332,8 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
332
332
333
333
let end_ty = self . annotations . get_type_or_void ( end, self . index ) ;
334
334
let counter_ty = self . annotations . get_type_or_void ( counter, self . index ) ;
335
- let ty = get_bigger_type ( self . index . get_type_or_panic ( "DINT" ) , counter_ty, self . index ) ;
336
- let ll_ty = self . llvm_index . find_associated_type ( ty . get_name ( ) ) . unwrap ( ) ;
335
+ let cast_target_ty = get_bigger_type ( self . index . get_type_or_panic ( DINT_TYPE ) , counter_ty, self . index ) ;
336
+ let cast_target_llty = self . llvm_index . find_associated_type ( cast_target_ty . get_name ( ) ) . unwrap ( ) ;
337
337
338
338
let step_ty = by_step. as_ref ( ) . map ( |it| {
339
339
self . register_debug_location ( it) ;
@@ -342,10 +342,10 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
342
342
343
343
let eval_step = || {
344
344
step_ty. map_or_else (
345
- || self . llvm . create_const_numeric ( & ll_ty , "1" , SourceLocation :: undefined ( ) ) ,
345
+ || self . llvm . create_const_numeric ( & cast_target_llty , "1" , SourceLocation :: undefined ( ) ) ,
346
346
|step_ty| {
347
347
let step = exp_gen. generate_expression ( by_step. as_ref ( ) . unwrap ( ) ) ?;
348
- Ok ( cast_if_needed ! ( exp_gen, ty , step_ty, step, None ) )
348
+ Ok ( cast_if_needed ! ( exp_gen, cast_target_ty , step_ty, step, None ) )
349
349
} ,
350
350
)
351
351
} ;
@@ -366,7 +366,9 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
366
366
let is_incrementing = builder. build_int_compare (
367
367
inkwell:: IntPredicate :: SGT ,
368
368
eval_step ( ) ?. into_int_value ( ) ,
369
- self . llvm . create_const_numeric ( & ll_ty, "0" , SourceLocation :: undefined ( ) ) ?. into_int_value ( ) ,
369
+ self . llvm
370
+ . create_const_numeric ( & cast_target_llty, "0" , SourceLocation :: undefined ( ) ) ?
371
+ . into_int_value ( ) ,
370
372
"is_incrementing" ,
371
373
) ;
372
374
builder. build_conditional_branch ( is_incrementing, predicate_incrementing, predicate_decrementing) ;
@@ -380,14 +382,14 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
380
382
381
383
let end = exp_gen. generate_expression_value ( end) . unwrap ( ) ;
382
384
let end_value = match end {
383
- super :: expression_generator :: ExpressionValue :: LValue ( ptr) => builder. build_load ( ptr, "" ) ,
384
- super :: expression_generator :: ExpressionValue :: RValue ( val) => val,
385
+ ExpressionValue :: LValue ( ptr) => builder. build_load ( ptr, "" ) ,
386
+ ExpressionValue :: RValue ( val) => val,
385
387
} ;
386
388
let counter_value = builder. build_load ( counter, "" ) ;
387
389
let cmp = builder. build_int_compare (
388
390
predicate,
389
- cast_if_needed ! ( exp_gen, ty , counter_ty, counter_value, None ) . into_int_value ( ) ,
390
- cast_if_needed ! ( exp_gen, ty , end_ty, end_value, None ) . into_int_value ( ) ,
391
+ cast_if_needed ! ( exp_gen, cast_target_ty , counter_ty, counter_value, None ) . into_int_value ( ) ,
392
+ cast_if_needed ! ( exp_gen, cast_target_ty , end_ty, end_value, None ) . into_int_value ( ) ,
391
393
"condition" ,
392
394
) ;
393
395
builder. build_conditional_branch ( cmp, loop_body, afterloop) ;
@@ -412,10 +414,13 @@ impl<'a, 'b> StatementCodeGenerator<'a, 'b> {
412
414
let counter_value = builder. build_load ( counter, "" ) ;
413
415
let inc = inkwell:: values:: BasicValue :: as_basic_value_enum ( & builder. build_int_add (
414
416
eval_step ( ) ?. into_int_value ( ) ,
415
- cast_if_needed ! ( exp_gen, ty , counter_ty, counter_value, None ) . into_int_value ( ) ,
417
+ cast_if_needed ! ( exp_gen, cast_target_ty , counter_ty, counter_value, None ) . into_int_value ( ) ,
416
418
"next" ,
417
419
) ) ;
418
- builder. build_store ( counter, cast_if_needed ! ( exp_gen, counter_ty, ty, inc, None ) . into_int_value ( ) ) ;
420
+ builder. build_store (
421
+ counter,
422
+ cast_if_needed ! ( exp_gen, counter_ty, cast_target_ty, inc, None ) . into_int_value ( ) ,
423
+ ) ;
419
424
420
425
// check condition
421
426
builder. build_conditional_branch ( is_incrementing, predicate_incrementing, predicate_decrementing) ;
0 commit comments