-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGEnum.cpp
More file actions
1438 lines (1288 loc) · 51.6 KB
/
Copy pathCGEnum.cpp
File metadata and controls
1438 lines (1288 loc) · 51.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 <iostream>
using namespace QLang;
using namespace std;
llvm::Value *CodeGen::genEnumConstruct( EnumConstructExpression *expr )
{
EnumDefinition *enumDef = expr->mEnumDef;
int variantIdx = expr->mVariantIndex;
if ( !enumHasPayload( enumDef ) )
{
// Plain enum without payload — just return the tag as i32
return llvm::ConstantInt::get(
llvm::Type::getInt32Ty( *mContext ), variantIdx );
}
// Tagged union enum: { i32 tag, [N x i8] payload }
llvm::StructType *enumType = getOrCreateEnumType( enumDef );
// Alloca the enum struct
llvm::AllocaInst *alloca = mBuilder->CreateAlloca( enumType, nullptr, "enum.tmp" );
// Store the tag
llvm::Value *tagPtr = mBuilder->CreateStructGEP( enumType, alloca, 0, "enum.tag.ptr" );
mBuilder->CreateStore(
llvm::ConstantInt::get( llvm::Type::getInt32Ty( *mContext ), variantIdx ),
tagPtr );
// Store the payload (if this variant has associated types)
auto &variant = enumDef->mVariants[variantIdx];
if ( !variant.mAssociatedTypes.empty() && !expr->mArgs.empty() )
{
llvm::Value *payloadPtr = mBuilder->CreateStructGEP(
enumType, alloca, 1, "enum.payload.ptr" );
// For each associated type, store the argument into the payload area
uint64_t offset = 0;
llvm::DataLayout dl( mModule.get() );
for ( size_t i = 0; i < variant.mAssociatedTypes.size() && i < expr->mArgs.size(); i++ )
{
llvm::Value *argVal = genExpression( expr->mArgs[i] );
if ( argVal == nullptr )
continue;
llvm::Type *argType = argVal->getType();
// Boxed enum payload: a payload whose declared type names an enum is
// stored as a POINTER to a heap-allocated copy, so recursive enums
// (enum Expr { add(Expr, Expr), ... }) have finite layout. The box
// carries a generated dtor that releases the boxed value's own
// refcounted payloads when the box's refcount hits zero. A GENERIC
// slot (built-in Option<T>/Result<T,E>) declares the erased param
// name, so it is boxed whenever the VALUE being stored is an enum
// struct — that is how `Result.ok(tree)` fits an arbitrarily large
// enum into the pointer-sized erased slot.
{
EnumDefinition *childEd = nullptr;
string assocName = variant.mAssociatedTypes[i]->getName();
auto boxIt = mEnumDefMap.find( assocName );
if ( boxIt != mEnumDefMap.end() )
childEd = boxIt->second;
if ( childEd == nullptr )
{
if ( auto *argSt = llvm::dyn_cast<llvm::StructType>( argType ) )
{
if ( argSt->hasName() &&
argSt->getName().str().substr( 0, 5 ) == "enum." )
{
auto it2 = mEnumDefMap.find(
argSt->getName().str().substr( 5 ) );
if ( it2 != mEnumDefMap.end() )
childEd = it2->second;
}
}
}
if ( childEd != nullptr && argType->isStructTy() )
{
llvm::StructType *childTy = getOrCreateEnumType( childEd );
llvm::Value *sizeVal = llvm::ConstantInt::get(
llvm::Type::getInt64Ty( *mContext ),
dl.getTypeAllocSize( childTy ) );
llvm::Function *boxDtor = getOrGenEnumBoxDtor( childEd );
llvm::Value *box;
if ( boxDtor != nullptr )
box = mBuilder->CreateCall( getOrDeclareRcAllocDtor(),
{ sizeVal, boxDtor }, "enum.box" );
else
box = mBuilder->CreateCall( getOrDeclareRcAlloc(),
{ sizeVal }, "enum.box" );
mBuilder->CreateStore( argVal, box );
// Copying from an existing owner (variable/field/index
// source) shares the inner refcounted payloads with the
// source, whose own release still runs — give the box its
// own references. A fresh temp (nested construct, call
// result) transfers ownership outright.
Expression *srcExpr = (Expression *)expr->mArgs[i];
bool srcOwner =
dynamic_cast<VariableExpression*>( srcExpr ) != nullptr ||
dynamic_cast<FieldAccessExpression*>( srcExpr ) != nullptr ||
dynamic_cast<IndexExpression*>( srcExpr ) != nullptr;
if ( srcOwner )
{
llvm::Function *pr = getOrGenEnumPayloadRetain( childEd );
if ( pr != nullptr )
mBuilder->CreateCall( pr, { box } );
}
llvm::Value *boxOffVal = llvm::ConstantInt::get(
llvm::Type::getInt64Ty( *mContext ), offset );
llvm::Type *payloadArrTy = enumType->getElementType( 1 );
llvm::Value *boxSlot = mBuilder->CreateGEP(
payloadArrTy, payloadPtr,
{ llvm::ConstantInt::get( llvm::Type::getInt64Ty( *mContext ), 0 ),
boxOffVal },
"enum.payload.box" );
mBuilder->CreateStore( box, boxSlot );
offset += 8;
continue;
}
}
// GEP into the payload byte array at the current offset
llvm::Value *offsetVal = llvm::ConstantInt::get(
llvm::Type::getInt64Ty( *mContext ), offset );
llvm::Type *payloadArrType = enumType->getElementType( 1 );
llvm::Value *bytePtr = mBuilder->CreateGEP(
payloadArrType, payloadPtr,
{ llvm::ConstantInt::get( llvm::Type::getInt64Ty( *mContext ), 0 ), offsetVal },
"enum.payload.byte" );
// Store the value through the byte pointer
mBuilder->CreateStore( argVal, bytePtr );
// If storing a refcounted payload (string/Array/struct) into the enum,
// the enum takes ownership — untrack the temporary so scope cleanup
// does not release it and leave the enum holding a dangling pointer
// (a use-after-free at the match/recv site). Key on the ARGUMENT's
// actual type: for a generic enum (built-in Option<T>/Result<T,E>) the
// declared associated type is a generic param (T/E), not
// "string"/"Array", so keying on the declared type would miss it.
if ( argType->isPointerTy() )
{
// The argument's source decides how the enum gets its reference,
// mirroring struct-literal field stores:
// - a temporary (call result, literal): transfer ownership by
// untracking it, so scope cleanup leaves it to the enum;
// - an existing owner (variable / field access): RETAIN, because
// the source's own scope release still runs — without the
// retain, e.g. `return Result.ok(a)` for a local Array leaves
// the enum pointing at freed memory (use-after-free at the
// match/`?` unwrap site).
Expression *argExpr = (Expression *)expr->mArgs[i];
bool srcIsExistingOwner =
dynamic_cast<VariableExpression*>( argExpr ) != nullptr ||
dynamic_cast<FieldAccessExpression*>( argExpr ) != nullptr;
if ( isStringType( expr->mArgs[i] ) )
{
if ( srcIsExistingOwner )
mBuilder->CreateCall( getOrDeclareStringRetain(), { argVal } );
else
untrackTempString( argVal );
}
else if ( isArrayType( expr->mArgs[i] ) )
{
if ( srcIsExistingOwner )
mBuilder->CreateCall( getOrDeclareArrayRetain(), { argVal } );
else
untrackTempArray( argVal );
}
else
{
std::string payloadTypeName;
if ( Type *rt = argExpr->getResolvedType() )
payloadTypeName = rt->getName();
if ( ( payloadTypeName.empty() || payloadTypeName == "var" ) &&
i < variant.mAssociatedTypes.size() )
payloadTypeName = variant.mAssociatedTypes[i]->getName();
if ( isUserStructType( payloadTypeName ) )
{
if ( srcIsExistingOwner )
mBuilder->CreateCall( getOrDeclareRcRetain(), { argVal } );
else
untrackTempStruct( argVal );
}
}
}
uint64_t typeSize = dl.getTypeAllocSize( argType );
if ( typeSize == 0 ) typeSize = 4; // fallback
offset += typeSize;
}
}
// Load and return the enum value
return mBuilder->CreateLoad( enumType, alloca, "enum.val" );
}
void CodeGen::trackEnumArgTemp( Expression *argExpr, llvm::Value *argVal,
Type *declParamType )
{
if ( argExpr == nullptr || argVal == nullptr )
return;
auto *st = llvm::dyn_cast<llvm::StructType>( argVal->getType() );
if ( st == nullptr || !st->hasName() )
return;
string stName = st->getName().str();
if ( stName.substr( 0, 5 ) != "enum." )
return;
// Only rvalue temps own an unreleased payload: a variable/field/index
// source's payload is already released where the variable was declared.
if ( dynamic_cast<VariableExpression*>( argExpr ) != nullptr ||
dynamic_cast<FieldAccessExpression*>( argExpr ) != nullptr ||
dynamic_cast<IndexExpression*>( argExpr ) != nullptr )
return;
auto it = mEnumDefMap.find( stName.substr( 5 ) );
if ( it == mEnumDefMap.end() )
return;
EnumDefinition *ed = it->second;
// Concrete instantiation: the callee's declared parameter type carries the
// type arguments (Option<string>); fall back to the argument's
// Sema-resolved type (a call result's function return type).
Type *concrete = declParamType;
if ( concrete == nullptr || concrete->getNumTypeParams() == 0 )
{
Type *rt = argExpr->getResolvedType();
if ( rt != nullptr )
concrete = rt;
}
if ( !enumHasRefcountedPayload( ed, concrete ) || mEnumScopeStack.empty() )
return;
llvm::AllocaInst *tmp = mBuilder->CreateAlloca( st, nullptr, "enumarg.tmp" );
mBuilder->CreateStore( argVal, tmp );
mEnumScopeStack.back().push_back( { tmp, ed, concrete } );
}
llvm::Function *CodeGen::getOrGenEnumBoxDtor( EnumDefinition *enumDef )
{
if ( enumDef == nullptr || !enumHasRefcountedPayload( enumDef, nullptr ) )
return nullptr;
string dtorName = "__enum_" + enumDef->getName() + "_box_dtor";
llvm::Function *fn = mModule->getFunction( dtorName );
if ( fn != nullptr && !fn->empty() )
return fn;
llvm::Type *ptrType = llvm::PointerType::get( *mContext, 0 );
if ( fn == nullptr )
{
llvm::FunctionType *ft = llvm::FunctionType::get(
llvm::Type::getVoidTy( *mContext ), { ptrType }, false );
fn = llvm::Function::Create(
ft, llvm::Function::InternalLinkage, dtorName, mModule.get() );
}
// Save/restore builder state while generating the dtor body
llvm::BasicBlock *savedBB = mBuilder->GetInsertBlock();
llvm::BasicBlock::iterator savedPt;
bool hadInsertPoint = ( savedBB != nullptr );
if ( hadInsertPoint )
savedPt = mBuilder->GetInsertPoint();
llvm::BasicBlock *entryBB = llvm::BasicBlock::Create( *mContext, "entry", fn );
mBuilder->SetInsertPoint( entryBB );
emitEnumPayloadReleaseFromPtr( fn->getArg( 0 ), enumDef, nullptr );
mBuilder->CreateRetVoid();
if ( hadInsertPoint )
mBuilder->SetInsertPoint( savedBB, savedPt );
return fn;
}
llvm::Function *CodeGen::getOrGenEnumPayloadRetain( EnumDefinition *enumDef )
{
if ( enumDef == nullptr || !enumHasRefcountedPayload( enumDef, nullptr ) )
return nullptr;
string fnName = "__enum_" + enumDef->getName() + "_payload_retain";
llvm::Function *fn = mModule->getFunction( fnName );
if ( fn != nullptr && !fn->empty() )
return fn;
llvm::Type *ptrType = llvm::PointerType::get( *mContext, 0 );
if ( fn == nullptr )
{
llvm::FunctionType *ft = llvm::FunctionType::get(
llvm::Type::getVoidTy( *mContext ), { ptrType }, false );
fn = llvm::Function::Create(
ft, llvm::Function::InternalLinkage, fnName, mModule.get() );
}
llvm::BasicBlock *savedBB = mBuilder->GetInsertBlock();
llvm::BasicBlock::iterator savedPt;
bool hadInsertPoint = ( savedBB != nullptr );
if ( hadInsertPoint )
savedPt = mBuilder->GetInsertPoint();
llvm::BasicBlock *entryBB = llvm::BasicBlock::Create( *mContext, "entry", fn );
mBuilder->SetInsertPoint( entryBB );
llvm::StructType *enumType = getOrCreateEnumType( enumDef );
llvm::Type *payloadArrType = enumType->getElementType( 1 );
llvm::DataLayout dl( mModule.get() );
llvm::Value *selfPtr = fn->getArg( 0 );
llvm::Value *tagPtr = mBuilder->CreateStructGEP( enumType, selfPtr, 0, "eret.tag.ptr" );
llvm::Value *tag = mBuilder->CreateLoad(
llvm::Type::getInt32Ty( *mContext ), tagPtr, "eret.tag" );
llvm::BasicBlock *mergeBB = llvm::BasicBlock::Create( *mContext, "eret.done", fn );
llvm::SwitchInst *sw = mBuilder->CreateSwitch( tag, mergeBB, enumDef->mVariants.size() );
for ( size_t vi = 0; vi < enumDef->mVariants.size(); vi++ )
{
auto &variant = enumDef->mVariants[vi];
bool hasRef = false;
for ( auto &at : variant.mAssociatedTypes )
{
string atn = resolveVariantPayloadType( (Type *)at, enumDef, nullptr )->getName();
if ( atn == "string" || atn == "Array" || atn == "Buffer" ||
isUserStructType( atn ) || mEnumDefMap.count( atn ) != 0 )
{
hasRef = true;
break;
}
}
if ( !hasRef )
continue;
llvm::BasicBlock *variantBB = llvm::BasicBlock::Create(
*mContext, "eret." + variant.mName, fn );
sw->addCase(
llvm::ConstantInt::get( llvm::Type::getInt32Ty( *mContext ), vi ),
variantBB );
mBuilder->SetInsertPoint( variantBB );
llvm::Value *payloadPtr = mBuilder->CreateStructGEP(
enumType, selfPtr, 1, "eret.payload" );
// Same offset walk as the release path.
uint64_t off = 0;
for ( auto &at : variant.mAssociatedTypes )
{
Type *resolved = resolveVariantPayloadType( (Type *)at, enumDef, nullptr );
string atn = resolved->getName();
bool isGenericSlot = false;
for ( auto &gp : enumDef->mGenericParams )
{
if ( gp.mName == ( (Type *)at )->getName() )
{
isGenericSlot = true;
break;
}
}
uint64_t slot;
if ( isGenericSlot || mEnumDefMap.count( atn ) != 0 )
slot = 8;
else
{
slot = dl.getTypeAllocSize( getLLVMType( resolved ) );
if ( slot == 0 ) slot = 4;
}
llvm::Value *bytePtr = mBuilder->CreateGEP(
payloadArrType, payloadPtr,
{ llvm::ConstantInt::get( llvm::Type::getInt64Ty( *mContext ), 0 ),
llvm::ConstantInt::get( llvm::Type::getInt64Ty( *mContext ), (int64_t)off ) },
"eret.payload.byte" );
llvm::Function *retainFn = nullptr;
if ( atn == "string" )
retainFn = getOrDeclareStringRetain();
else if ( atn == "Array" )
retainFn = getOrDeclareArrayRetain();
else if ( atn == "Buffer" )
retainFn = getOrDeclareBufferRetain();
else if ( isUserStructType( atn ) || mEnumDefMap.count( atn ) != 0 )
retainFn = getOrDeclareRcRetain();
if ( retainFn != nullptr )
{
llvm::Value *val = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), bytePtr, "eret.val" );
mBuilder->CreateCall( retainFn, { val } );
}
off += slot;
}
mBuilder->CreateBr( mergeBB );
}
mBuilder->SetInsertPoint( mergeBB );
mBuilder->CreateRetVoid();
if ( hadInsertPoint )
mBuilder->SetInsertPoint( savedBB, savedPt );
return fn;
}
// ---- Match codegen (Task 53) ----
llvm::Value *CodeGen::genMatchExpression( MatchExpression *expr )
{
// Snapshot the temp-string stack so we can release ONLY the temporaries
// created while evaluating the subject (e.g. string-literal args to a
// subject call like `match env.get("X")`), not any pre-existing live temps
// from an enclosing expression (which the arms may still need). See the
// flush below.
size_t tempMarkBeforeSubject = mTempStrings.size();
llvm::Value *subject = genExpression( expr->mSubject );
if ( subject == nullptr )
return nullptr;
llvm::Function *func = mBuilder->GetInsertBlock()->getParent();
llvm::BasicBlock *mergeBB = llvm::BasicBlock::Create( *mContext, "matchend", func );
// Determine if this is a tagged union enum match
bool isEnumStruct = false;
EnumDefinition *matchedEnum = nullptr;
llvm::StructType *enumStructType = nullptr;
llvm::Value *tagVal = nullptr;
llvm::AllocaInst *subjectAlloca = nullptr;
if ( auto *st = llvm::dyn_cast<llvm::StructType>( subject->getType() ) )
{
// Check if this struct type corresponds to a known enum
string stName = st->hasName() ? st->getName().str() : "";
// Enum types are named "enum.EnumName"
if ( stName.substr( 0, 5 ) == "enum." )
{
string enumName = stName.substr( 5 );
auto enumIt = mEnumDefMap.find( enumName );
if ( enumIt != mEnumDefMap.end() )
{
isEnumStruct = true;
matchedEnum = enumIt->second;
enumStructType = st;
// Store the subject in an alloca so we can GEP into it
subjectAlloca = mBuilder->CreateAlloca( st, nullptr, "match.enum" );
mBuilder->CreateStore( subject, subjectAlloca );
// If the subject is a TEMPORARY enum value (an rvalue such as a
// function/method call result — e.g. `match divide(a,b)` or
// `match ch.recv()`), nothing else owns or scope-releases it. When
// its variant carries a refcounted payload (string/Array/Buffer/
// struct — e.g. Result<int,string>.err), register this alloca on the
// enum scope stack so the payload is released at scope/return exit
// via emitEnumPayloadRelease, exactly as an enum *variable* is.
// Without this the payload leaks. A variable/field subject is NOT
// registered here — it is already tracked at its own declaration, so
// registering again would double-free.
bool subjectIsTemporary =
dynamic_cast<VariableExpression*>( (Expression*)expr->mSubject ) == nullptr &&
dynamic_cast<FieldAccessExpression*>( (Expression*)expr->mSubject ) == nullptr;
if ( subjectIsTemporary && !mEnumScopeStack.empty() )
{
// Concrete instantiation of the subject (e.g. Result<int,string>)
// so a generic-param payload (built-in Option/Result) resolves to
// its concrete refcounted type for release.
Type *subjConcrete = ( (Expression*)expr->mSubject )->getResolvedType();
if ( enumHasRefcountedPayload( matchedEnum, subjConcrete ) )
mEnumScopeStack.back().push_back(
{ subjectAlloca, matchedEnum, subjConcrete } );
}
// Extract the tag: GEP to field 0, load i32
llvm::Value *tagPtr = mBuilder->CreateStructGEP(
st, subjectAlloca, 0, "match.tag.ptr" );
tagVal = mBuilder->CreateLoad(
llvm::Type::getInt32Ty( *mContext ), tagPtr, "match.tag" );
}
}
}
// The subject expression is fully evaluated and (for an enum) copied into
// subjectAlloca, so any temporary strings created while evaluating it — e.g.
// string-literal arguments to a subject call like `match env.get("X")` —
// must be released HERE, in this single pre-branch block, so they are freed
// on every arm path. Deferring to the arms let the first arm's
// releaseTempStrings() consume them, leaking on the other arms / fall-through
// (surfaced by codegen_env.b: the arg literal leaked on the none path).
//
// SURGICAL: release only the temps created DURING subject evaluation
// (indices >= tempMarkBeforeSubject), never pre-existing temps from an
// enclosing expression (e.g. a sibling string-literal arg in `foo("lit",
// match ...)`), which the arms/rest of the statement may still use. Only for
// the enum case, where the subject value is already copied into
// subjectAlloca; the payload string (some(v)) is untracked at construction
// and released via mEnumScopeStack, so it is not among these temps.
if ( isEnumStruct &&
mBuilder->GetInsertBlock()->getTerminator() == nullptr )
{
for ( size_t k = tempMarkBeforeSubject; k < mTempStrings.size(); k++ )
mBuilder->CreateCall( getOrDeclareStringRelease(), { mTempStrings[k] } );
if ( mTempStrings.size() > tempMarkBeforeSubject )
mTempStrings.resize( tempMarkBeforeSubject );
}
// Find the default (wildcard) arm, or create a default that falls through
int wildcardIdx = -1;
for ( size_t i = 0; i < expr->mArms.size(); i++ )
{
if ( expr->mArms[i].mIsWildcard )
{
wildcardIdx = static_cast<int>( i );
break;
}
}
// Create basic blocks for each arm
std::vector<llvm::BasicBlock*> armBBs;
for ( size_t i = 0; i < expr->mArms.size(); i++ )
{
string name = "match.arm." + to_string( i );
armBBs.push_back( llvm::BasicBlock::Create( *mContext, name, func ) );
}
// Default block: either the wildcard arm or the merge block
llvm::BasicBlock *defaultBB = ( wildcardIdx >= 0 ) ? armBBs[wildcardIdx] : mergeBB;
// Determine the switch value: either the subject itself (i32) or the extracted tag
llvm::Value *switchVal = isEnumStruct ? tagVal : subject;
// Build switch for integer/tag subjects
if ( switchVal != nullptr && switchVal->getType()->isIntegerTy() )
{
// Count actual cases for the switch
int numCases = 0;
for ( size_t i = 0; i < expr->mArms.size(); i++ )
{
if ( !expr->mArms[i].mIsWildcard )
numCases++;
}
llvm::SwitchInst *switchInst = mBuilder->CreateSwitch(
switchVal, defaultBB, numCases );
for ( size_t i = 0; i < expr->mArms.size(); i++ )
{
if ( expr->mArms[i].mIsWildcard )
continue;
const string &pattern = expr->mArms[i].mPattern;
int64_t patternVal = 0;
// Try to parse as integer
bool isNumeric = !pattern.empty() &&
( isdigit( pattern[0] ) || pattern[0] == '-' );
if ( isNumeric )
{
patternVal = stoll( pattern );
}
else
{
// Named pattern (e.g., "ok", "err", "none") — use variant index
bool found = false;
// If we know the specific enum, search only in it
if ( matchedEnum != nullptr )
{
for ( size_t v = 0; v < matchedEnum->mVariants.size(); v++ )
{
if ( matchedEnum->mVariants[v].mName == pattern )
{
patternVal = static_cast<int64_t>( v );
found = true;
break;
}
}
}
if ( !found )
{
// Search all registered enums
for ( auto &ep : mEnumDefMap )
{
for ( size_t v = 0; v < ep.second->mVariants.size(); v++ )
{
if ( ep.second->mVariants[v].mName == pattern )
{
patternVal = static_cast<int64_t>( v );
found = true;
break;
}
}
if ( found )
break;
}
}
if ( !found )
{
// Use arm index as fallback
patternVal = static_cast<int64_t>( i );
}
}
switchInst->addCase(
llvm::ConstantInt::get(
llvm::cast<llvm::IntegerType>( switchVal->getType() ),
patternVal ),
armBBs[i] );
}
}
else if ( subject->getType()->isPointerTy() &&
isStringType( (Expression *)expr->mSubject ) )
{
// String subject: chain __blang_string_equals_cstr checks against each
// string-literal pattern, in arm order; no match falls to the default.
// (Previously ANY non-integer subject silently branched to the default
// arm — the design spec's own `match command { "start" {...} ... }`
// example always took the wildcard.)
std::vector<size_t> strArms;
for ( size_t i = 0; i < expr->mArms.size(); i++ )
{
if ( !expr->mArms[i].mIsWildcard && expr->mArms[i].mPatternIsString )
strArms.push_back( i );
}
llvm::Function *eqFn = getOrDeclareStringEqualsCstr();
for ( size_t k = 0; k < strArms.size(); k++ )
{
size_t i = strArms[k];
const string &pat = expr->mArms[i].mPattern;
llvm::Value *litPtr = mBuilder->CreateGlobalStringPtr( pat, "match.pat" );
llvm::Value *litLen = llvm::ConstantInt::get(
llvm::Type::getInt64Ty( *mContext ), (int64_t)pat.size() );
llvm::Value *isEq = mBuilder->CreateCall(
eqFn, { subject, litPtr, litLen }, "match.streq" );
llvm::BasicBlock *noMatchBB = ( k + 1 < strArms.size() )
? llvm::BasicBlock::Create( *mContext, "match.strnext", func )
: defaultBB;
mBuilder->CreateCondBr( isEq, armBBs[i], noMatchBB );
if ( k + 1 < strArms.size() )
mBuilder->SetInsertPoint( noMatchBB );
}
if ( strArms.empty() )
mBuilder->CreateBr( defaultBB );
}
else
{
// Non-integer, non-string subject — just branch to default
mBuilder->CreateBr( defaultBB );
}
// Expression-mode result slot. The alloca (and its zero-init) go into the
// function's ENTRY block so they dominate every arm store and the merge
// load; the concrete LLVM type is taken from the first arm value produced.
llvm::AllocaInst *resultAlloca = nullptr;
llvm::Type *resultTy = nullptr;
bool resIsString = false, resIsArray = false, resIsStruct = false;
string resKindName;
if ( expr->mExprMode && expr->getResolvedType() != nullptr )
resKindName = resolvedTypeName( expr->getResolvedType() );
// Generate code for each arm body
for ( size_t i = 0; i < expr->mArms.size(); i++ )
{
mBuilder->SetInsertPoint( armBBs[i] );
// The binding lives in the arm's block scope (statement form) or in the
// arm's own scope (expression form).
Scope *armScope = ( expr->mArms[i].mBody != nullptr )
? (Scope *)expr->mArms[i].mBody->mScope
: (Scope *)expr->mArms[i].mScope;
// If the arm has bindings, extract the payload value(s) from the enum
// struct. Payloads are laid out sequentially in the payload byte array
// (construction advances by DataLayout alloc size per argument), so
// binding j loads at the sum of the preceding bindings' sizes.
if ( !expr->mArms[i].mBindingNames.empty() )
{
if ( isEnumStruct && matchedEnum != nullptr && subjectAlloca != nullptr )
{
// Find the variant for this arm's pattern
int variantIdx = -1;
for ( size_t v = 0; v < matchedEnum->mVariants.size(); v++ )
{
if ( matchedEnum->mVariants[v].mName == expr->mArms[i].mPattern )
{
variantIdx = static_cast<int>( v );
break;
}
}
// GEP to the payload area (field 1)
llvm::Value *payloadPtr = mBuilder->CreateStructGEP(
enumStructType, subjectAlloca, 1, "match.payload.ptr" );
llvm::Type *payloadArrType = enumStructType->getElementType( 1 );
llvm::DataLayout dl( mModule.get() );
uint64_t payloadOffset = 0;
for ( size_t bi = 0; bi < expr->mArms[i].mBindingNames.size(); bi++ )
{
const string &bname = expr->mArms[i].mBindingNames[bi];
llvm::Type *bindType = llvm::Type::getInt32Ty( *mContext ); // default
// Prefer the binding variable's Sema-resolved type: for a generic
// enum (built-in Option<T>/Result<T,E>) Sema substitutes the concrete
// type argument from the subject (e.g. err payload -> string), which
// the raw variant associated type ("T"/"E") does not carry. Fall back
// to the variant's declared associated type for non-generic enums.
Type *bindQType = nullptr;
if ( armScope != nullptr )
{
Symbol *bs = armScope->findSymbol( bname );
if ( auto *bv = dynamic_cast<VariableDefinition*>( bs ) )
bindQType = bv->getVariableType();
}
// Boxed enum payload: the declared associated type names an
// enum, so the slot holds a POINTER to the child value.
string bindQName;
if ( bindQType != nullptr && bindQType->getName() != "var" )
bindQName = bindQType->getName();
else if ( variantIdx >= 0 &&
bi < matchedEnum->mVariants[variantIdx].mAssociatedTypes.size() )
bindQName = matchedEnum->mVariants[variantIdx]
.mAssociatedTypes[bi]->getName();
bool isBoxedEnum = ( mEnumDefMap.count( bindQName ) != 0 );
if ( isBoxedEnum )
{
bindType = getOrCreateEnumType( mEnumDefMap[bindQName] );
}
else if ( bindQType != nullptr && bindQType->getName() != "var" )
{
bindType = getLLVMType( bindQType );
}
else if ( variantIdx >= 0 &&
bi < matchedEnum->mVariants[variantIdx].mAssociatedTypes.size() )
{
bindType = getLLVMType(
matchedEnum->mVariants[variantIdx].mAssociatedTypes[bi] );
}
// GEP to this payload's byte offset and load as the expected type
llvm::Value *bytePtr = mBuilder->CreateGEP(
payloadArrType, payloadPtr,
{ llvm::ConstantInt::get( llvm::Type::getInt64Ty( *mContext ), 0 ),
llvm::ConstantInt::get( llvm::Type::getInt64Ty( *mContext ),
(int64_t)payloadOffset ) },
"match.payload.byte" );
llvm::Value *payloadVal;
if ( isBoxedEnum )
{
// Load the box pointer, then copy the boxed enum value
// out — the binding is a by-value BORROW of the box's
// contents (the box stays owned by the subject).
llvm::Value *boxPtr = mBuilder->CreateLoad(
llvm::PointerType::get( *mContext, 0 ), bytePtr,
"match.payload.boxptr" );
payloadVal = mBuilder->CreateLoad(
bindType, boxPtr, "match.payload.boxed" );
}
else
{
payloadVal = mBuilder->CreateLoad(
bindType, bytePtr, "match.payload.val" );
}
// Create alloca for the binding variable
llvm::AllocaInst *bindAlloca = mBuilder->CreateAlloca(
bindType, nullptr, bname );
mBuilder->CreateStore( payloadVal, bindAlloca );
// Register in variable map
if ( armScope != nullptr )
{
Symbol *bindSym = armScope->findSymbol( bname );
if ( auto *bindVar = dynamic_cast<VariableDefinition*>( bindSym ) )
mVariableMap[bindVar] = bindAlloca;
}
// Advance by this payload's size — must mirror the
// construction walk (boxed enum slots are pointer-sized).
if ( isBoxedEnum )
payloadOffset += 8;
else
{
uint64_t sz = dl.getTypeAllocSize( bindType );
if ( sz == 0 ) sz = 4;
payloadOffset += sz;
}
}
}
else
{
// Non-enum binding: store the subject value directly
const string &bname = expr->mArms[i].mBindingNames[0];
llvm::Type *bindType = subject->getType();
llvm::AllocaInst *bindAlloca = mBuilder->CreateAlloca(
bindType, nullptr, bname );
mBuilder->CreateStore( subject, bindAlloca );
// Register in variable map
if ( armScope != nullptr )
{
Symbol *bindSym = armScope->findSymbol( bname );
if ( auto *bindVar = dynamic_cast<VariableDefinition*>( bindSym ) )
mVariableMap[bindVar] = bindAlloca;
}
}
}
if ( expr->mExprMode )
{
// Expression-form arm: evaluate the single expression, hand the
// result slot an OWNED reference (mirroring the return-statement
// policy: retain borrowed sources — variable/field/index reads —
// and take over ownership of fresh temps by untracking them), then
// store and fall through to the merge.
size_t sMark = mTempStrings.size();
size_t aMark = mTempArrays.size();
size_t tMark = mTempStructs.size();
llvm::Value *armVal = ( expr->mArms[i].mValue != nullptr )
? genExpression( expr->mArms[i].mValue ) : nullptr;
if ( armVal != nullptr )
{
if ( resultTy == nullptr )
{
resultTy = armVal->getType();
llvm::IRBuilder<> eb( &func->getEntryBlock(),
func->getEntryBlock().begin() );
resultAlloca = eb.CreateAlloca( resultTy, nullptr, "match.result" );
eb.CreateStore( llvm::Constant::getNullValue( resultTy ), resultAlloca );
}
Expression *ve = (Expression *)expr->mArms[i].mValue;
bool borrowed =
dynamic_cast<VariableExpression*>( ve ) != nullptr ||
dynamic_cast<FieldAccessExpression*>( ve ) != nullptr ||
dynamic_cast<IndexExpression*>( ve ) != nullptr;
if ( resKindName == "string" || isStringType( ve ) )
{
resIsString = true;
if ( borrowed )
mBuilder->CreateCall( getOrDeclareStringRetain(), { armVal } );
else
untrackTempString( armVal );
}
else if ( resKindName == "Array" || isArrayType( ve ) )
{
resIsArray = true;
if ( borrowed )
mBuilder->CreateCall( getOrDeclareArrayRetain(), { armVal } );
else
untrackTempArray( armVal );
}
else if ( isUserStructType( resKindName ) )
{
resIsStruct = true;
if ( borrowed )
mBuilder->CreateCall( getOrDeclareRcRetain(), { armVal } );
else
untrackTempStruct( armVal );
}
if ( armVal->getType() != resultTy &&
armVal->getType()->isIntegerTy() && resultTy->isIntegerTy() )
armVal = mBuilder->CreateIntCast( armVal, resultTy, true, "match.arm.cast" );
if ( armVal->getType() == resultTy )
mBuilder->CreateStore( armVal, resultAlloca );
}
// Release temps created while evaluating THIS arm, inside this
// arm's block — deferring to the statement end would emit release
// calls whose operands do not dominate them (defined only in this
// arm's basic block).
if ( mBuilder->GetInsertBlock()->getTerminator() == nullptr )
{
for ( size_t k = sMark; k < mTempStrings.size(); k++ )
mBuilder->CreateCall( getOrDeclareStringRelease(), { mTempStrings[k] } );
for ( size_t k = aMark; k < mTempArrays.size(); k++ )
mBuilder->CreateCall( getOrDeclareArrayRelease(), { mTempArrays[k] } );
for ( size_t k = tMark; k < mTempStructs.size(); k++ )
mBuilder->CreateCall( getOrDeclareRcRelease(), { mTempStructs[k] } );
}
if ( mTempStrings.size() > sMark ) mTempStrings.resize( sMark );
if ( mTempArrays.size() > aMark ) mTempArrays.resize( aMark );
if ( mTempStructs.size() > tMark ) mTempStructs.resize( tMark );
}
else if ( expr->mArms[i].mBody != nullptr )
genBlock( expr->mArms[i].mBody );
if ( mBuilder->GetInsertBlock()->getTerminator() == nullptr )
mBuilder->CreateBr( mergeBB );
}
// Move the merge block to the end of the function. It was created before the
// arm blocks, so without this it sits in the middle of the block list; for
// main() the function finalizer resets the insert point to llvmFunc->back()
// (an arm), leaving matchend unterminated when the match is the function's
// tail and every arm returns. Making matchend the last block lets the
// finalizer add the implicit return to it (or following statements flow into
// it normally when the match is not the tail).
mergeBB->moveAfter( &func->back() );
// Continue at merge
mBuilder->SetInsertPoint( mergeBB );
// Expression mode: yield the selected arm's value. The loaded value is an
// OWNED reference (each arm retained/transferred into the slot), so hand it
// to the enclosing statement as a tracked temporary — a binding untracks
// and takes ownership; a discarding statement releases it at statement end.
if ( expr->mExprMode )
{
if ( resultAlloca == nullptr )
return nullptr;
llvm::Value *resVal = mBuilder->CreateLoad( resultTy, resultAlloca, "match.value" );
if ( resIsString )
trackTempString( resVal );
else if ( resIsArray )
trackTempArray( resVal );
else if ( resIsStruct )
trackTempStruct( resVal );
return resVal;
}
return nullptr;
}
// ---- Try operator codegen (Task 54) ----
EnumDefinition *CodeGen::resolveExpressionEnumDef( Expression *expr )
{
// Resolve the QLang-level type name of the expression to find its EnumDefinition.
std::string typeName;
if ( auto *call = dynamic_cast<CallExpression*>( expr ) )
{
if ( call->mFunction != nullptr && call->mFunction->getReturnType() != nullptr )
typeName = call->mFunction->getReturnType()->getName();
}
else if ( auto *varExpr = dynamic_cast<VariableExpression*>( expr ) )
{
if ( varExpr->mVariable != nullptr && varExpr->mVariable->getVariableType() != nullptr )
typeName = varExpr->mVariable->getVariableType()->getName();
}
else if ( auto *methodCall = dynamic_cast<MethodCallExpression*>( expr ) )
{
(void)methodCall;
// Method return types are harder to resolve statically; fall through
}
if ( !typeName.empty() )
{
auto it = mEnumDefMap.find( typeName );
if ( it != mEnumDefMap.end() )
return it->second;
}
return nullptr;
}
llvm::Value *CodeGen::genTryExpression( TryExpression *expr )
{
// Generate the operand expression (e.g., might_fail())
llvm::Value *result = genExpression( expr->mOperand );
if ( result == nullptr )
return nullptr;
// Try to resolve the operand's enum definition
EnumDefinition *enumDef = resolveExpressionEnumDef( expr->mOperand );
// If we can't determine the enum type, fall back to pass-through
if ( enumDef == nullptr || !enumHasPayload( enumDef ) )
return result;
// Find success variant (ok/some) and error variant (err/none)
int successIdx = -1;
int errorIdx = -1;
for ( size_t i = 0; i < enumDef->mVariants.size(); i++ )
{
const std::string &vname = enumDef->mVariants[i].mName;
if ( vname == "ok" || vname == "some" )
successIdx = static_cast<int>( i );
else if ( vname == "err" || vname == "none" )
errorIdx = static_cast<int>( i );
}
// If we can't identify the variants, pass through
if ( successIdx < 0 || errorIdx < 0 )
return result;
llvm::StructType *enumType = getOrCreateEnumType( enumDef );