-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGExpressions.cpp
More file actions
1673 lines (1523 loc) · 59.6 KB
/
Copy pathCGExpressions.cpp
File metadata and controls
1673 lines (1523 loc) · 59.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "CodeGen.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Constants.h"
#include "FormatString.h"
#include <iostream>
using namespace QLang;
using namespace std;
llvm::Value *CodeGen::genConstInteger( ConstInteger *ci )
{
return llvm::ConstantInt::get(
llvm::Type::getInt32Ty( *mContext ), ci->mValue, true );
}
llvm::Value *CodeGen::genConstFloat( ConstFloat *cf )
{
return llvm::ConstantFP::get(
llvm::Type::getDoubleTy( *mContext ), cf->mValue );
}
llvm::Value *CodeGen::genConstString( ConstString *cs )
{
// Create the global string data (null-terminated for C compat)
llvm::Constant *strData = mBuilder->CreateGlobalStringPtr( cs->mValue, "str.data" );
// Call __blang_string_create_static(data, length)
llvm::Function *createStatic = getOrDeclareStringCreateStatic();
llvm::Value *lenVal = llvm::ConstantInt::get(
llvm::Type::getInt64Ty( *mContext ), cs->mValue.size() );
llvm::Value *result = mBuilder->CreateCall( createStatic, { strData, lenVal }, "str" );
trackTempString( result );
return result;
}
llvm::Value *CodeGen::genConstChar( ConstChar *cc )
{
uint8_t charVal = 0;
if ( !cc->mValue.empty() )
charVal = static_cast<uint8_t>( cc->mValue[0] );
return llvm::ConstantInt::get(
llvm::Type::getInt8Ty( *mContext ), charVal );
}
llvm::Value *CodeGen::genVariableExpression( VariableExpression *var )
{
VariableDefinition *varDef = var->mVariable;
// Check for own variables crossing spawn boundaries (before variable lookup,
// because own variables are excluded from spawn captures)
if ( mSpawnOuterOwnVars.count( varDef ) )
{
cerr << "Error: own variable '" << varDef->getName()
<< "' cannot be captured by spawn block (use shared or sync instead)" << endl;
mHasError = true;
return nullptr;
}
auto it = mVariableMap.find( varDef );
if ( it == mVariableMap.end() )
{
cerr << "CodeGen: undefined variable '" << varDef->getName() << "'" << endl;
return nullptr;
}
// U6: use-after-move / move analysis was lifted into the semantic pass
// (Sema.cpp), which runs before codegen in all build modes and reports located
// diagnostics (and correctly clears moved state on reassignment). Codegen no
// longer re-checks moves here — sema is authoritative.
llvm::AllocaInst *alloca = it->second;
OwnershipQualifier ownership = varDef->getOwnership();
if ( ownership == OwnershipQualifier::kOwnership_Shared )
{
// Shared: alloca holds a pointer to heap data (immutable, no lock needed).
// Load the pointer, then load the actual value through it.
llvm::Value *heapPtr = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), alloca, varDef->getName() + ".ptr" );
llvm::Type *dataType = getLLVMType( varDef->getVariableType() );
return mBuilder->CreateLoad( dataType, heapPtr, varDef->getName() );
}
if ( ownership == OwnershipQualifier::kOwnership_Sync )
{
// Sync: alloca holds a pointer to heap data (mutex-protected).
// Lock before read, unlock after read to prevent data races.
llvm::Value *heapPtr = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), alloca, varDef->getName() + ".ptr" );
llvm::Type *dataType = getLLVMType( varDef->getVariableType() );
mBuilder->CreateCall( getOrDeclareSyncLock(), { heapPtr } );
llvm::Value *val = mBuilder->CreateLoad( dataType, heapPtr, varDef->getName() );
mBuilder->CreateCall( getOrDeclareSyncUnlock(), { heapPtr } );
return val;
}
return mBuilder->CreateLoad( alloca->getAllocatedType(), alloca, varDef->getName() );
}
llvm::Value *CodeGen::genCallExpression( CallExpression *call )
{
FunctionDefinition *funcDef = call->mFunction;
// Handle builtin functions (print/println)
if ( funcDef->isBuiltin() )
{
if ( funcDef->getName() == "print" )
{
genPrintCall( call, false );
return nullptr;
}
if ( funcDef->getName() == "println" )
{
genPrintCall( call, true );
return nullptr;
}
if ( funcDef->getName() == "to_json" )
{
return genToJsonCall( call );
}
}
// Handle @format annotation: validate format string at call site
for ( const auto &ann : funcDef->getAnnotations() )
{
if ( ann.mName == "format" && !call->mParams.empty() )
{
auto *fmtConst = dynamic_cast<ConstString*>( (Expression*)call->mParams[0] );
if ( fmtConst != nullptr )
{
// Parse format string and validate arg count
int phCount = 0;
const std::string &fmt = fmtConst->mValue;
for ( size_t fi = 0; fi < fmt.size(); fi++ )
{
if ( fmt[fi] == '{' && fi + 1 < fmt.size() && fmt[fi + 1] == '{' )
{ fi++; continue; }
if ( fmt[fi] == '{' )
phCount++;
}
int extraArgs = (int)call->mParams.size() - funcDef->getNumberParams();
if ( phCount != extraArgs )
{
reportError( call, "@format function '" + funcDef->getName() +
"': format string has " + to_string( phCount ) +
" placeholder(s) but " + to_string( extraArgs ) +
" extra argument(s) provided" );
return nullptr;
}
}
break;
}
}
// Handle generic function instantiation. When the caller wrote no explicit
// <...> list, infer the type arguments from the argument expressions
// (identity(w) with a string w == identity<string>(w)); a generic call whose
// arguments cannot bind every parameter is a LOUD error — previously it fell
// through to "undefined function" without setting mHasError, so the call was
// silently dropped and the target variable read uninitialized memory.
if ( funcDef->isGeneric() && call->mTypeArgs.empty() )
{
if ( !inferCallTypeArgs( call, funcDef ) )
{
reportError( call, "cannot infer type arguments for generic function '" +
funcDef->getName() + "' — call it with explicit type arguments, e.g. " +
funcDef->getName() + "<int>(...)" );
return nullptr;
}
// Constraint checking for INFERRED type arguments: Sema checks
// explicit-arg calls (REQ-008), but inferred arguments only exist
// after inference here. Structural: the bound struct must implement
// every required method by name (the arity/shape check happened at
// the protocol's impl site). Non-struct / unknown bindings are left
// unchecked, mirroring Sema's stance.
const auto &cgps = funcDef->getGenericParams();
for ( size_t gi = 0; gi < cgps.size() && gi < call->mTypeArgs.size(); gi++ )
{
if ( cgps[gi].mConstraint.empty() )
continue;
auto pIt = mProtocolDefMap.find( cgps[gi].mConstraint );
if ( pIt == mProtocolDefMap.end() )
continue;
auto sIt = mStructDefMap.find( ( (Type *)call->mTypeArgs[gi] )->getName() );
if ( sIt == mStructDefMap.end() )
continue;
for ( auto &req : pIt->second->getRequiredMethods() )
{
if ( req == nullptr )
continue;
bool found = false;
for ( auto &m : sIt->second->getMethods() )
if ( m != nullptr && m->getName() == req->getName() )
{
found = true;
break;
}
if ( !found )
{
reportError( call, "inferred type '" +
( (Type *)call->mTypeArgs[gi] )->getName() +
"' does not satisfy constraint '" + cgps[gi].mConstraint +
"' on generic parameter '" + cgps[gi].mName +
"' of '" + funcDef->getName() + "': missing method '" +
req->getName() + "'" );
return nullptr;
}
}
}
}
if ( !call->mTypeArgs.empty() && funcDef->isGeneric() )
{
llvm::Function *genFunc = instantiateGenericFunction( funcDef, call->mTypeArgs );
if ( genFunc == nullptr )
{
reportError( call, "failed to instantiate generic function '" +
funcDef->getName() + "'" );
return nullptr;
}
// Generate argument values
std::vector<llvm::Value*> args;
for ( size_t pi = 0; pi < call->mParams.size(); pi++ )
{
llvm::Value *argVal = genExpression( call->mParams[pi] );
if ( argVal == nullptr )
return nullptr;
// A payload-carrying enum rvalue argument owns its payload with no
// releasing owner — register it for scope-exit release (ledger #7).
VariableDefinition *pd = ( pi < (size_t)funcDef->getNumberParams() )
? funcDef->getParam( pi ) : nullptr;
trackEnumArgTemp( call->mParams[pi], argVal,
pd != nullptr ? pd->getVariableType() : nullptr );
// Integer width coercion to the instantiated parameter type,
// mirroring the non-generic call path — without it a `bool` param
// (i1) receiving a bool literal (codegen'd i32) fails IR
// verification (`pick<T>(T a, T b, bool first)`).
if ( pi < genFunc->arg_size() )
{
llvm::Type *paramType = genFunc->getFunctionType()->getParamType( pi );
if ( paramType->isIntegerTy() && argVal->getType()->isIntegerTy() &&
paramType->getIntegerBitWidth() < argVal->getType()->getIntegerBitWidth() )
argVal = mBuilder->CreateTrunc( argVal, paramType, "garg.trunc" );
else if ( paramType->isIntegerTy() && argVal->getType()->isIntegerTy() &&
paramType->getIntegerBitWidth() > argVal->getType()->getIntegerBitWidth() )
argVal = mBuilder->CreateSExt( argVal, paramType, "garg.ext" );
}
args.push_back( argVal );
}
if ( genFunc->getReturnType()->isVoidTy() )
{
mBuilder->CreateCall( genFunc, args );
return nullptr;
}
llvm::Value *callResult = mBuilder->CreateCall( genFunc, args, "calltmp" );
// Track refcounted returns as temporaries, keyed on the CONCRETE return
// type — the declared name is an erased param ("T") for calls like
// pick_first<string>, so callReturnTypeName maps it through the type
// arguments. (See the non-generic path below for the ownership model.)
string genRetName = callReturnTypeName( call );
if ( genRetName == "string" )
trackTempString( callResult );
else if ( genRetName == "Array" )
trackTempArray( callResult );
else if ( isUserStructType( genRetName ) )
trackTempStruct( callResult );
return callResult;
}
// Look up the LLVM function
llvm::Function *llvmFunc = nullptr;
auto it = mFunctionMap.find( funcDef );
if ( it != mFunctionMap.end() )
{
llvmFunc = it->second;
}
else
{
// Try by name in the module — check mangled name first, then original
if ( !call->mMangledName.empty() )
llvmFunc = mModule->getFunction( call->mMangledName );
if ( llvmFunc == nullptr )
llvmFunc = mModule->getFunction( funcDef->getName() );
}
// If still not found, auto-declare extern functions (e.g. from .bmod imports)
if ( llvmFunc == nullptr && funcDef->isExtern() )
{
llvmFunc = genFunction( funcDef );
}
if ( llvmFunc == nullptr )
{
// Loud: without mHasError the compile exited 0 with the call silently
// dropped, leaving the consumer reading uninitialized memory.
reportError( call, "undefined function '" + funcDef->getName() + "'" +
( !call->mMangledName.empty()
? " (mangled: " + call->mMangledName + ")" : "" ) );
return nullptr;
}
// Generate argument values with FFI conversion for extern functions
std::vector<llvm::Value*> args;
for ( size_t argIdx = 0; argIdx < call->mParams.size(); argIdx++ )
{
llvm::Value *argVal = genExpression( call->mParams[argIdx] );
if ( argVal == nullptr )
return nullptr;
// A payload-carrying enum rvalue argument owns its payload with no
// releasing owner — register it for scope-exit release (ledger #7).
{
VariableDefinition *pd = ( argIdx < (size_t)funcDef->getNumberParams() )
? funcDef->getParam( argIdx ) : nullptr;
trackEnumArgTemp( call->mParams[argIdx], argVal,
pd != nullptr ? pd->getVariableType() : nullptr );
}
// FFI: if calling extern fn and param is cstring but arg is string,
// extract the .data field from BlangString*
if ( funcDef->isExtern() && argIdx < (size_t)funcDef->getNumberParams() )
{
VariableDefinition *paramDef = funcDef->getParam( argIdx );
if ( paramDef != nullptr &&
paramDef->getVariableType() != nullptr &&
paramDef->getVariableType()->getName() == "cstring" &&
isStringType( call->mParams[argIdx] ) )
{
// GEP into BlangString struct field 0 (data pointer) and load
// BlangString: { char*, i64, i64, i32 }
llvm::StructType *bsType = llvm::StructType::get( *mContext,
{ llvm::PointerType::get( *mContext, 0 ),
llvm::Type::getInt64Ty( *mContext ),
llvm::Type::getInt64Ty( *mContext ),
llvm::Type::getInt32Ty( *mContext ) } );
llvm::Value *dataPtr = mBuilder->CreateStructGEP(
bsType, argVal, 0, "str.data.ptr" );
argVal = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), dataPtr, "str.data" );
}
// FFI: if param is carray and arg is Array, extract .data field
if ( paramDef->getVariableType()->getName() == "carray" &&
isArrayType( call->mParams[argIdx] ) )
{
// GEP into BlangArray struct field 0 (data pointer) and load
// BlangArray: { void*, i64, i64, i32, i32 }
llvm::StructType *baType = llvm::StructType::get( *mContext,
{ llvm::PointerType::get( *mContext, 0 ),
llvm::Type::getInt64Ty( *mContext ),
llvm::Type::getInt64Ty( *mContext ),
llvm::Type::getInt32Ty( *mContext ),
llvm::Type::getInt32Ty( *mContext ) } );
llvm::Value *dataPtr = mBuilder->CreateStructGEP(
baType, argVal, 0, "arr.data.ptr" );
argVal = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), dataPtr, "arr.data" );
}
}
// Fn-type argument: split {ptr, ptr} into two separate LLVM args
if ( argIdx < (size_t)funcDef->getNumberParams() )
{
VariableDefinition *paramDef = funcDef->getParam( argIdx );
if ( paramDef != nullptr && paramDef->getVariableType()->isFunctionType() )
{
// Check if argVal is a named function reference (needs thunk wrapping)
auto *argVarExpr = dynamic_cast<VariableExpression*>( (Expression*)call->mParams[argIdx] );
if ( argVarExpr == nullptr )
{
// It's a lambda or other expression that already produces {ptr, ptr}
// Check if it's a named function being passed by name
auto *argCallExpr = dynamic_cast<CallExpression*>( (Expression*)call->mParams[argIdx] );
(void)argCallExpr;
}
llvm::Value *fnPtr = mBuilder->CreateExtractValue( argVal, 0, "cb.fn" );
llvm::Value *ctxPtr = mBuilder->CreateExtractValue( argVal, 1, "cb.ctx" );
args.push_back( fnPtr );
args.push_back( ctxPtr );
continue;
}
}
// Integer type coercion: widen or narrow to match parameter type
if ( llvmFunc != nullptr && argIdx < llvmFunc->arg_size() )
{
llvm::Type *paramType = llvmFunc->getFunctionType()->getParamType( argIdx );
if ( paramType->isIntegerTy() && argVal->getType()->isIntegerTy() &&
paramType->getIntegerBitWidth() > argVal->getType()->getIntegerBitWidth() )
{
argVal = mBuilder->CreateSExt( argVal, paramType, "arg.ext" );
}
else if ( paramType->isIntegerTy() && argVal->getType()->isIntegerTy() &&
paramType->getIntegerBitWidth() < argVal->getType()->getIntegerBitWidth() )
{
argVal = mBuilder->CreateTrunc( argVal, paramType, "arg.trunc" );
}
}
args.push_back( argVal );
}
// Move semantics: mark own variables as moved when passed to own parameters
for ( size_t i = 0; i < call->mParams.size() && i < (size_t)funcDef->getNumberParams(); i++ )
{
VariableDefinition *paramDef = funcDef->getParam( i );
if ( paramDef != nullptr && paramDef->getOwnership() == OwnershipQualifier::kOwnership_Own )
{
auto *argVarExpr = dynamic_cast<VariableExpression*>( (Expression*)call->mParams[i] );
if ( argVarExpr != nullptr )
{
VariableDefinition *srcDef = argVarExpr->getVariable();
if ( srcDef->getOwnership() == OwnershipQualifier::kOwnership_Own )
{
if ( mInsideLoop )
{
cerr << "Error: cannot move own variable '" << srcDef->getName()
<< "' inside a loop (would move on each iteration)" << endl;
mHasError = true;
return nullptr;
}
mMovedVariables.insert( srcDef );
}
}
}
}
if ( llvmFunc->getReturnType()->isVoidTy() )
{
mBuilder->CreateCall( llvmFunc, args );
return nullptr;
}
llvm::Value *callResult = mBuilder->CreateCall( llvmFunc, args, "calltmp" );
// Track string-returning function calls as temps
if ( funcDef->getReturnType() != nullptr &&
funcDef->getReturnType()->getName() == "string" )
{
trackTempString( callResult );
}
// Track struct-returning function calls as temporaries so the fresh
// refcount-1 heap struct (allocated by __blang_rc_alloc inside the callee,
// then untracked at its `return`) is released at the end of the enclosing
// statement — unless it is stored into a variable / struct field / enum
// payload / returned, each of which untracks it (ownership transfers).
// This mirrors the temp-tracking of struct literals (genStructLiteral /
// genConstructExpression) so an rvalue struct from a call and one from a
// literal have identical ARC lifetimes. Without this, an unstored struct
// rvalue — e.g. `make_info(...).has_flag()` — leaks.
if ( funcDef->getReturnType() != nullptr &&
isUserStructType( funcDef->getReturnType()->getName() ) )
{
trackTempStruct( callResult );
}
// Track Array<T>-returning calls as temporaries (see genReturnStatement for
// the matching ownership contract): a function returns an owned array
// reference, released at statement end unless it is stored / transferred.
if ( funcDef->getReturnType() != nullptr &&
funcDef->getReturnType()->getName() == "Array" )
{
trackTempArray( callResult );
}
return callResult;
}
llvm::Value *CodeGen::genOperationsExpression( OperationsExpression *ops )
{
const string &op = ops->mOperation;
// Short-circuit logical operators (&&, ||): the RHS must be evaluated ONLY
// when the LHS does not already determine the result — so its side effects
// are skipped otherwise. This MUST run before the eager operand evaluation
// below (which would force the RHS). Lowers to a branch + i1 phi.
if ( op == "&&" || op == "||" )
{
bool isAnd = ( op == "&&" );
llvm::Function *func = mBuilder->GetInsertBlock()->getParent();
// Evaluate the LHS and coerce to i1 (!= 0 for int, != 0.0 for float).
llvm::Value *lhs = genExpression( ops->mOp1 );
if ( lhs == nullptr )
return nullptr;
llvm::Value *lBool = lhs->getType()->isFloatingPointTy()
? mBuilder->CreateFCmpONE( lhs, llvm::ConstantFP::get( lhs->getType(), 0.0 ), "lbool" )
: mBuilder->CreateICmpNE( lhs, llvm::ConstantInt::get( lhs->getType(), 0 ), "lbool" );
llvm::BasicBlock *entryBB = mBuilder->GetInsertBlock();
llvm::BasicBlock *rhsBB = llvm::BasicBlock::Create(
*mContext, isAnd ? "land.rhs" : "lor.rhs", func );
llvm::BasicBlock *mergeBB = llvm::BasicBlock::Create(
*mContext, isAnd ? "land.end" : "lor.end", func );
// &&: LHS true -> evaluate RHS, else short-circuit to false.
// ||: LHS true -> short-circuit to true, else evaluate RHS.
if ( isAnd )
mBuilder->CreateCondBr( lBool, rhsBB, mergeBB );
else
mBuilder->CreateCondBr( lBool, mergeBB, rhsBB );
// RHS block: evaluate the RHS (its side effects run only here) and
// coerce to i1. Refcounted temporaries born during the RHS (e.g. the
// string argument of `s.has("a")`) must be released INSIDE this block,
// not deferred to statement scope: the statement-end release lands in the
// merge block, which the RHS block does not dominate when the LHS
// short-circuits — so the release would use a value defined only on the
// taken edge ("instruction does not dominate all uses" → IR-verify ICE).
// Snapshot the temp lists, then flush what the RHS added, in this block.
size_t tmMarkStr = mTempStrings.size(), tmMarkLam = mTempLambdaCtxs.size();
size_t tmMarkStruct = mTempStructs.size(), tmMarkArr = mTempArrays.size();
mBuilder->SetInsertPoint( rhsBB );
llvm::Value *rhs = genExpression( ops->mOp2 );
if ( rhs == nullptr )
return nullptr;
llvm::Value *rBool = rhs->getType()->isFloatingPointTy()
? mBuilder->CreateFCmpONE( rhs, llvm::ConstantFP::get( rhs->getType(), 0.0 ), "rbool" )
: mBuilder->CreateICmpNE( rhs, llvm::ConstantInt::get( rhs->getType(), 0 ), "rbool" );
// Release RHS-born temporaries here (the RHS result is an i1, never a
// tracked temp), confining them to the RHS edge.
auto flushTempsSince = [&]( std::vector<llvm::Value*> &v, size_t mark,
llvm::FunctionCallee fn ) {
for ( size_t i = mark; i < v.size(); ++i )
mBuilder->CreateCall( fn, { v[i] } );
v.resize( mark );
};
flushTempsSince( mTempStrings, tmMarkStr, getOrDeclareStringRelease() );
flushTempsSince( mTempLambdaCtxs, tmMarkLam, getOrDeclareLambdaCtxRelease() );
flushTempsSince( mTempStructs, tmMarkStruct, getOrDeclareRcRelease() );
flushTempsSince( mTempArrays, tmMarkArr, getOrDeclareArrayRelease() );
// Nested control flow in the RHS may have changed the current block, so
// snapshot the real predecessor for the phi.
llvm::BasicBlock *rhsEndBB = mBuilder->GetInsertBlock();
mBuilder->CreateBr( mergeBB );
// Merge: pick the short-circuit constant (false for &&, true for ||)
// from the entry edge, or the computed RHS bool from the RHS edge.
mBuilder->SetInsertPoint( mergeBB );
llvm::Type *i1Ty = llvm::Type::getInt1Ty( *mContext );
llvm::PHINode *phi = mBuilder->CreatePHI( i1Ty, 2, isAnd ? "landtmp" : "lortmp" );
phi->addIncoming( llvm::ConstantInt::get( i1Ty, isAnd ? 0 : 1 ), entryBB );
phi->addIncoming( rBool, rhsEndBB );
return phi;
}
llvm::Value *left = genExpression( ops->mOp1 );
llvm::Value *right = genExpression( ops->mOp2 );
if ( left == nullptr || right == nullptr )
return nullptr;
// `byte` is unsigned (byte->int conversion already zero-extends, see
// codegen_byte.b). Widen byte operands with ZExt (not SExt) and use a
// logical right shift, so byte arithmetic/print stays unsigned (0-255).
bool leftIsByte = isByteExpression( ops->mOp1 );
bool rightIsByte = isByteExpression( ops->mOp2 );
// Type promotion for mixed-width operands
if ( left->getType() != right->getType() )
{
if ( left->getType()->isIntegerTy() && right->getType()->isIntegerTy() )
{
unsigned leftBits = left->getType()->getIntegerBitWidth();
unsigned rightBits = right->getType()->getIntegerBitWidth();
if ( leftBits < rightBits )
{
if ( leftBits == 1 || leftIsByte )
left = mBuilder->CreateZExt( left, right->getType(), "bpromote" );
else
left = mBuilder->CreateSExt( left, right->getType(), "promote" );
}
else
{
if ( rightBits == 1 || rightIsByte )
right = mBuilder->CreateZExt( right, left->getType(), "bpromote" );
else
right = mBuilder->CreateSExt( right, left->getType(), "promote" );
}
}
else if ( left->getType()->isFloatingPointTy() && right->getType()->isFloatingPointTy() )
{
if ( left->getType()->isFloatTy() && right->getType()->isDoubleTy() )
left = mBuilder->CreateFPExt( left, right->getType(), "fpromote" );
else if ( left->getType()->isDoubleTy() && right->getType()->isFloatTy() )
right = mBuilder->CreateFPExt( right, left->getType(), "fpromote" );
}
else if ( left->getType()->isIntegerTy() && right->getType()->isFloatingPointTy() )
{
left = mBuilder->CreateSIToFP( left, right->getType(), "itofp" );
}
else if ( left->getType()->isFloatingPointTy() && right->getType()->isIntegerTy() )
{
right = mBuilder->CreateSIToFP( right, left->getType(), "itofp" );
}
}
bool isFloat = left->getType()->isFloatingPointTy();
// String operations: check if operands are string type
bool isString = isStringType( ops->mOp1 ) && isStringType( ops->mOp2 );
// Array operations: check if operands are array type
bool isArray = isArrayType( ops->mOp1 ) && isArrayType( ops->mOp2 );
// Array concatenation
if ( isArray && op == "+" )
return mBuilder->CreateCall( getOrDeclareArrayConcat(), { left, right }, "arrcat" );
// String concatenation
if ( isString && op == "+" )
{
llvm::Value *result = mBuilder->CreateCall( getOrDeclareStringConcat(), { left, right }, "strcat" );
trackTempString( result );
return result;
}
// String comparison
if ( isString && op == "==" )
return mBuilder->CreateCall( getOrDeclareStringEquals(), { left, right }, "streq" );
if ( isString && op == "!=" )
{
llvm::Value *eq = mBuilder->CreateCall( getOrDeclareStringEquals(), { left, right }, "streq" );
return mBuilder->CreateNot( eq, "strne" );
}
// String relational operators are LEXICOGRAPHIC via __blang_string_compare
// (returns <0/0/>0), not a pointer compare. Without this, `<`/`>`/`<=`/`>=`
// on strings fell through to an integer compare of the string POINTERS —
// meaningless ordering (surfaced by U5's generic sort<string>).
if ( isString && ( op == "<" || op == ">" || op == "<=" || op == ">=" ) )
{
llvm::Value *cmp = mBuilder->CreateCall(
getOrDeclareStringCompare(), { left, right }, "strcmp" );
llvm::Value *zero = llvm::ConstantInt::get( llvm::Type::getInt32Ty( *mContext ), 0 );
if ( op == "<" ) return mBuilder->CreateICmpSLT( cmp, zero, "strlt" );
if ( op == ">" ) return mBuilder->CreateICmpSGT( cmp, zero, "strgt" );
if ( op == "<=" ) return mBuilder->CreateICmpSLE( cmp, zero, "strle" );
return mBuilder->CreateICmpSGE( cmp, zero, "strge" );
}
// Arithmetic
if ( op == "+" ) return isFloat ? mBuilder->CreateFAdd( left, right, "addtmp" ) : mBuilder->CreateAdd( left, right, "addtmp" );
if ( op == "-" ) return isFloat ? mBuilder->CreateFSub( left, right, "subtmp" ) : mBuilder->CreateSub( left, right, "subtmp" );
if ( op == "*" ) return isFloat ? mBuilder->CreateFMul( left, right, "multmp" ) : mBuilder->CreateMul( left, right, "multmp" );
if ( op == "/" ) return isFloat ? mBuilder->CreateFDiv( left, right, "divtmp" ) : mBuilder->CreateSDiv( left, right, "divtmp" );
if ( op == "%" ) return isFloat ? mBuilder->CreateFRem( left, right, "modtmp" ) : mBuilder->CreateSRem( left, right, "modtmp" );
// Bitwise (integer only)
if ( op == "&" ) return mBuilder->CreateAnd( left, right, "andtmp" );
if ( op == "|" ) return mBuilder->CreateOr( left, right, "ortmp" );
if ( op == "^" ) return mBuilder->CreateXor( left, right, "xortmp" );
if ( op == "<<" ) return mBuilder->CreateShl( left, right, "shltmp" );
if ( op == ">>" ) return ( leftIsByte )
? mBuilder->CreateLShr( left, right, "shrtmp" )
: mBuilder->CreateAShr( left, right, "shrtmp" );
// Comparisons (produce i1)
if ( op == "==" ) return isFloat ? mBuilder->CreateFCmpOEQ( left, right, "eqtmp" ) : mBuilder->CreateICmpEQ( left, right, "eqtmp" );
if ( op == "!=" ) return isFloat ? mBuilder->CreateFCmpONE( left, right, "netmp" ) : mBuilder->CreateICmpNE( left, right, "netmp" );
if ( op == "<" ) return isFloat ? mBuilder->CreateFCmpOLT( left, right, "lttmp" ) : mBuilder->CreateICmpSLT( left, right, "lttmp" );
if ( op == ">" ) return isFloat ? mBuilder->CreateFCmpOGT( left, right, "gttmp" ) : mBuilder->CreateICmpSGT( left, right, "gttmp" );
if ( op == "<=" ) return isFloat ? mBuilder->CreateFCmpOLE( left, right, "letmp" ) : mBuilder->CreateICmpSLE( left, right, "letmp" );
if ( op == ">=" ) return isFloat ? mBuilder->CreateFCmpOGE( left, right, "getmp" ) : mBuilder->CreateICmpSGE( left, right, "getmp" );
// Logical operators &&/|| are handled at the top of this function with
// short-circuit branching (they never reach here).
cerr << "CodeGen: unknown binary operator '" << op << "'" << endl;
return nullptr;
}
llvm::Value *CodeGen::genAssignmentExpression( AssignmentExpression *assign )
{
VariableDefinition *varDef = assign->mVariable;
auto it = mVariableMap.find( varDef );
if ( it == mVariableMap.end() )
{
cerr << "CodeGen: undefined variable '" << varDef->getName() << "'" << endl;
return nullptr;
}
llvm::AllocaInst *alloca = it->second;
const string &op = assign->mOperation;
OwnershipQualifier ownership = varDef->getOwnership();
// Reject assignment to shared variables — shared values are immutable
if ( ownership == OwnershipQualifier::kOwnership_Shared )
{
reportError( assign, "cannot assign to shared variable '" +
varDef->getName() + "' — shared values are immutable" );
return nullptr;
}
// Sync `=` assignment: evaluate RHS inside the lock to prevent TOCTOU races.
// This ensures expressions like `counter = counter + 1` are atomic.
if ( ownership == OwnershipQualifier::kOwnership_Sync && op == "=" )
{
llvm::Value *heapPtr = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), alloca, varDef->getName() + ".ptr" );
mBuilder->CreateCall( getOrDeclareSyncLock(), { heapPtr } );
llvm::Value *rhs = genExpression( assign->mValue );
if ( rhs == nullptr )
{
mBuilder->CreateCall( getOrDeclareSyncUnlock(), { heapPtr } );
return nullptr;
}
mBuilder->CreateStore( rhs, heapPtr );
mBuilder->CreateCall( getOrDeclareSyncUnlock(), { heapPtr } );
return rhs;
}
llvm::Value *rhs = genExpression( assign->mValue );
if ( rhs == nullptr )
return nullptr;
// Sync compound assignment: lock, read-modify-write, unlock
if ( ownership == OwnershipQualifier::kOwnership_Sync )
{
llvm::Value *heapPtr = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), alloca, varDef->getName() + ".ptr" );
llvm::Type *dataType = getLLVMType( varDef->getVariableType() );
mBuilder->CreateCall( getOrDeclareSyncLock(), { heapPtr } );
llvm::Value *current = mBuilder->CreateLoad( dataType, heapPtr, "cur" );
llvm::Value *result = nullptr;
if ( op == "+=" ) result = mBuilder->CreateAdd( current, rhs, "addassign" );
else if ( op == "-=" ) result = mBuilder->CreateSub( current, rhs, "subassign" );
else if ( op == "*=" ) result = mBuilder->CreateMul( current, rhs, "mulassign" );
else if ( op == "/=" ) result = mBuilder->CreateSDiv( current, rhs, "divassign" );
else result = current;
if ( result != nullptr )
mBuilder->CreateStore( result, heapPtr );
mBuilder->CreateCall( getOrDeclareSyncUnlock(), { heapPtr } );
return result;
}
if ( op == "=" )
{
// If reassigning a string variable, release the old value first
if ( varDef->getVariableType() != nullptr &&
varDef->getVariableType()->getName() == "string" )
{
llvm::Value *oldVal = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), alloca, "str.old" );
mBuilder->CreateCall( getOrDeclareStringRelease(), { oldVal } );
// The RHS is now owned by this variable — untrack from temps
untrackTempString( rhs );
}
// If reassigning a struct variable, release the old value and untrack the new
if ( varDef->getVariableType() != nullptr &&
isUserStructType( varDef->getVariableType()->getName() ) )
{
llvm::Value *oldVal = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), alloca, "struct.old" );
mBuilder->CreateCall( getOrDeclareRcRelease(), { oldVal } );
untrackTempStruct( rhs );
}
// If reassigning an array variable, release the old value and untrack the
// new (the variable now owns it; released at scope exit).
if ( varDef->getVariableType() != nullptr &&
varDef->getVariableType()->getName() == "Array" )
{
llvm::Value *oldVal = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), alloca, "arr.old" );
mBuilder->CreateCall( getOrDeclareArrayRelease(), { oldVal } );
untrackTempArray( rhs );
}
// If reassigning an enum variable whose payloads are refcounted (boxed
// children, strings), release the OLD value's payloads before the
// store — otherwise `d = Expr.add(d, ...)` leaks the previous tree.
// The RHS was evaluated above, so a self-referencing RHS already
// retained anything it copied from the old value.
if ( varDef->getVariableType() != nullptr )
{
auto edIt = mEnumDefMap.find( varDef->getVariableType()->getName() );
if ( edIt != mEnumDefMap.end() &&
enumHasRefcountedPayload( edIt->second, varDef->getVariableType() ) )
{
emitEnumPayloadRelease( alloca, edIt->second,
varDef->getVariableType() );
}
}
// Coerce integer RHS to the destination width before storing. Without this,
// assigning a bool literal (`true`/`false` — codegen'd as i32) to an i1 bool
// variable emits `store i32 into i1*`, a 4-byte write into a 1-byte stack slot
// that corrupts adjacent locals (e.g. a loop counter → infinite loop). The
// var-decl initializer path already coerces; the assignment path must too.
llvm::Value *storeVal = rhs;
llvm::Type *destTy = alloca->getAllocatedType();
if ( rhs->getType()->isIntegerTy() && destTy->isIntegerTy() &&
rhs->getType() != destTy )
{
if ( destTy->isIntegerTy( 1 ) )
{
// Any nonzero value becomes true.
storeVal = mBuilder->CreateICmpNE(
rhs, llvm::ConstantInt::get( rhs->getType(), 0 ), "assign.tobool" );
}
else
{
unsigned srcBits = rhs->getType()->getIntegerBitWidth();
unsigned dstBits = destTy->getIntegerBitWidth();
storeVal = ( dstBits < srcBits )
? mBuilder->CreateTrunc( rhs, destTy, "assign.trunc" )
: mBuilder->CreateSExt( rhs, destTy, "assign.sext" );
}
}
mBuilder->CreateStore( storeVal, alloca );
// Move semantics: if assigning an own variable from another own variable,
// mark the source as moved
if ( ownership == OwnershipQualifier::kOwnership_Own )
{
auto *srcVarExpr = dynamic_cast<VariableExpression*>( (Expression*)assign->mValue );
if ( srcVarExpr != nullptr )
{
VariableDefinition *srcDef = srcVarExpr->getVariable();
if ( srcDef->getOwnership() == OwnershipQualifier::kOwnership_Own )
{
if ( mInsideLoop )
{
cerr << "Error: cannot move own variable '" << srcDef->getName()
<< "' inside a loop (would move on each iteration)" << endl;
mHasError = true;
return nullptr;
}
mMovedVariables.insert( srcDef );
}
}
}
return rhs;
}
// Compound assignment: load current value, apply operation, store result
llvm::Value *current = mBuilder->CreateLoad(
alloca->getAllocatedType(), alloca, "cur" );
llvm::Value *result = nullptr;
if ( op == "+=" ) result = mBuilder->CreateAdd( current, rhs, "addassign" );
else if ( op == "-=" ) result = mBuilder->CreateSub( current, rhs, "subassign" );
else if ( op == "*=" ) result = mBuilder->CreateMul( current, rhs, "mulassign" );
else if ( op == "/=" ) result = mBuilder->CreateSDiv( current, rhs, "divassign" );
else if ( op == "%=" ) result = mBuilder->CreateSRem( current, rhs, "modassign" );
else if ( op == "^=" ) result = mBuilder->CreateXor( current, rhs, "xorassign" );
else
{
cerr << "CodeGen: unknown assignment operator '" << op << "'" << endl;
return nullptr;
}
mBuilder->CreateStore( result, alloca );
return result;
}
llvm::Value *CodeGen::genUnaryExpression( UnaryExpression *unary )
{
llvm::Value *operand = genExpression( unary->mOperand );
if ( operand == nullptr )
return nullptr;
const string &op = unary->mOperation;
if ( op == "-" )
{
// Float/double negation must use fneg — CreateNeg emits an integer
// `sub 0, x`, which is invalid on floating-point operands (surfaced by
// U4's first float codegen test: `math.fabs(-2.5)` produced an illegal
// `sub (double 0.0, double 2.5)` constexpr).
if ( operand->getType()->isFloatingPointTy() )
return mBuilder->CreateFNeg( operand, "negtmp" );
return mBuilder->CreateNeg( operand, "negtmp" );
}
if ( op == "!" )
{
llvm::Value *boolVal = mBuilder->CreateICmpNE(
operand,
llvm::ConstantInt::get( operand->getType(), 0 ),
"tobool" );
llvm::Value *notVal = mBuilder->CreateXor(
boolVal,
llvm::ConstantInt::getTrue( *mContext ),
"nottmp" );
return mBuilder->CreateZExt( notVal, operand->getType(), "lnot" );
}
if ( op == "~" )
return mBuilder->CreateNot( operand, "bnottmp" );
cerr << "CodeGen: unknown unary operator '" << op << "'" << endl;
return nullptr;
}
// ---- Struct codegen (Tasks 26-27) ----
llvm::AllocaInst *CodeGen::getExpressionAddress( Expression *expr )
{
if ( auto *ve = dynamic_cast<VariableExpression*>( expr ) )
{
auto it = mVariableMap.find( ve->mVariable );
if ( it != mVariableMap.end() )
return it->second;
}
return nullptr;
}
// The struct a string-interpolation part denotes, or nullptr when the part is
// not a struct value. Used to route struct parts through Printable rather than
// handing a struct pointer to the string runtime as if it were a BlangString.
// The LLVM function implementing a struct's `to_string`. Prefers the emitted
// symbol, then falls back to the method's entry in mFunctionMap — the symbol may
// not exist yet when the CALLER is generated before the conformance impl block
// that defines it (method emission follows source order).
llvm::Function *CodeGen::lookupToStringFn( StructDefinition *sd )
{
if ( sd == nullptr )
return nullptr;
if ( llvm::Function *fn = mModule->getFunction( sd->getName() + "_to_string" ) )
return fn;
for ( auto &msp : sd->mMethods )
{
FunctionDefinition *m = const_cast<FunctionDefinition*>(
(const FunctionDefinition*)msp );
if ( m == nullptr || m->getName() != "to_string" )
continue;
auto it = mFunctionMap.find( m );
if ( it != mFunctionMap.end() )
return it->second;
// The method is declared on the struct but its LLVM function has not
// been created yet: method bodies are generated in source order, so a
// caller earlier in the file reaches here before the conformance impl
// block that defines to_string. Forward-declare it; the definition lands
// later in this same module.
llvm::Type *ptrTy = llvm::PointerType::get( *mContext, 0 );
llvm::FunctionType *ft = llvm::FunctionType::get( ptrTy, { ptrTy }, false );
return llvm::Function::Create( ft, llvm::Function::ExternalLinkage,
sd->getName() + "_to_string", mModule.get() );
}
return nullptr;
}
// Only a VARIABLE or a FIELD ACCESS can be an interpolation part today — the
// parser builds nothing else — and receiverStructDef handles both, including a
// `self` base. Anything else is not a struct value.
StructDefinition *CodeGen::structDefForInterpolationPart( Expression *part )
{
if ( dynamic_cast<VariableExpression*>( part ) == nullptr &&
dynamic_cast<FieldAccessExpression*>( part ) == nullptr )
return nullptr;
return receiverStructDef( part );
}
// Does this struct render through Printable? Explicit `impl Printable` always
// counts; a structural `to_string` counts only for a type defined in THIS module
// — an imported type must state conformance through the .bmod record (D16), so a
// private to_string that vanishes under `pub` filtering does not silently qualify
// it. The single source of truth for both the print and interpolation paths.
bool CodeGen::structIsPrintable( StructDefinition *sd )
{
if ( sd == nullptr )
return false;
for ( const auto &proto : sd->getConformedProtocols() )
if ( proto == "Printable" )
return true;
if ( !sd->isFromInterface() )
{
for ( auto &m : sd->getMethods() )
if ( m->getName() == "to_string" )
return true;
}
return false;
}
// The self pointer for a struct receiver, taken from the value's ADDRESS. A
// loaded value is wrong for `shared`/`sync`, whose variable holds a heap pointer
// that genVariableExpression loads twice (yielding the struct's first 8 bytes as
// a pointer). A receiver with no addressable slot — a field access or a call
// result — has genExpression yield the heap pointer directly, which is correct.
llvm::Value *CodeGen::structSelfPointer( Expression *argExpr )
{
llvm::AllocaInst *addr = getExpressionAddress( argExpr );
if ( addr != nullptr && addr->getAllocatedType()->isPointerTy() )
return mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), addr, "self.ptr" );