@@ -265,15 +265,6 @@ Ref translateExpressionInner(
265265 std::vector<Ref> argExprRefs = { consumerRef, elementRef };
266266
267267 buildCall (globalState, functionState, bodyBuilder, consumerMethod, argExprRefs);
268- //
269- // auto consumerInterfaceMT = dynamic_cast<InterfaceKind*>(consumerType->kind);
270- // assert(consumerInterfaceMT);
271- // int indexInEdge = globalState->getInterfaceMethodIndex(consumerInterfaceMT, consumerMethod);
272- // auto methodFunctionPtrLE =
273- // globalState->getRegion(consumerType)
274- // ->getInterfaceMethodFunctionPtr(functionState, bodyBuilder, consumerType, consumerRef, indexInEdge);
275- // buildInterfaceCall(
276- // globalState, functionState, bodyBuilder, consumerMethod, methodFunctionPtrLE, argExprRefs, 0);
277268 });
278269
279270 if (arrayType->ownership == Ownership::OWN) {
@@ -289,20 +280,150 @@ Ref translateExpressionInner(
289280 assert (false );
290281 }
291282
292-
293283 globalState->getRegion (consumerType)
294284 ->dealias (
295285 AFL (" DestroySSAIntoF" ), functionState, builder, consumerType, consumerRef);
296286
297287 return makeEmptyTupleRef (globalState);
298- } else if (auto destroyRuntimeSizedArrayIntoFunction = dynamic_cast <DestroyRuntimeSizedArray*>(expr)) {
299- buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
300- auto consumerType = destroyRuntimeSizedArrayIntoFunction->consumerType ;
301- auto arrayKind = destroyRuntimeSizedArrayIntoFunction->arrayKind ;
302- auto arrayExpr = destroyRuntimeSizedArrayIntoFunction->arrayExpr ;
303- auto consumerExpr = destroyRuntimeSizedArrayIntoFunction->consumerExpr ;
304- auto consumerMethod = destroyRuntimeSizedArrayIntoFunction->consumerMethod ;
305- auto arrayType = destroyRuntimeSizedArrayIntoFunction->arrayType ;
288+ } else if (auto pushRuntimeSizedArray = dynamic_cast <PushRuntimeSizedArray*>(expr)) {
289+ buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
290+ auto arrayExpr = pushRuntimeSizedArray->arrayExpr ;
291+ auto arrayType = pushRuntimeSizedArray->arrayType ;
292+ auto arrayMT = dynamic_cast <RuntimeSizedArrayT*>(arrayType->kind );
293+ assert (arrayMT);
294+ bool arrayKnownLive = true ;
295+ auto newcomerExpr = pushRuntimeSizedArray->newcomerExpr ;
296+ auto newcomerType = pushRuntimeSizedArray->newcomerType ;
297+ bool newcomerKnownLive = true ;
298+
299+ auto arrayRef = translateExpression (globalState, functionState, blockState, builder, arrayExpr);
300+ globalState->getRegion (arrayType)
301+ ->checkValidReference (FL (), functionState, builder, arrayType, arrayRef);
302+
303+ auto arrayLenRef =
304+ globalState->getRegion (arrayType)
305+ ->getRuntimeSizedArrayLength (
306+ functionState, builder, arrayType, arrayRef, arrayKnownLive);
307+ auto arrayLenLE =
308+ globalState->getRegion (globalState->metalCache ->i32Ref )
309+ ->checkValidReference (FL (),
310+ functionState, builder, globalState->metalCache ->i32Ref , arrayLenRef);
311+
312+ auto arrayCapacityRef =
313+ globalState->getRegion (arrayType)
314+ ->getRuntimeSizedArrayCapacity (
315+ functionState, builder, arrayType, arrayRef, arrayKnownLive);
316+ auto arrayCapacityLE =
317+ globalState->getRegion (globalState->metalCache ->i32Ref )
318+ ->checkValidReference (FL (),
319+ functionState, builder, globalState->metalCache ->i32Ref , arrayCapacityRef);
320+
321+ auto hasSpaceLE = LLVMBuildICmp (builder, LLVMIntULT, arrayLenLE, arrayCapacityLE, " hasSpace" );
322+ buildIf (globalState, functionState, builder, hasSpaceLE, [globalState](LLVMBuilderRef bodyBuilder) {
323+ buildPrint (globalState, bodyBuilder, " Error: Runtime-sized array has no room for new element!" );
324+ });
325+
326+ auto newcomerRef = translateExpression (globalState, functionState, blockState, builder, newcomerExpr);
327+ globalState->getRegion (newcomerType)
328+ ->checkValidReference (FL (), functionState, builder, newcomerType, newcomerRef);
329+
330+ globalState->getRegion (arrayType)
331+ ->pushRuntimeSizedArrayNoBoundsCheck (functionState, builder, arrayType, arrayMT, arrayRef, arrayKnownLive, arrayLenRef, newcomerRef);
332+
333+ globalState->getRegion (arrayType)
334+ ->dealias (
335+ AFL (" pushRuntimeSizedArrayNoBoundsCheck" ), functionState, builder, arrayType, arrayRef);
336+
337+ return makeEmptyTupleRef (globalState);
338+ } else if (auto popRuntimeSizedArray = dynamic_cast <PopRuntimeSizedArray*>(expr)) {
339+ buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
340+ auto arrayExpr = popRuntimeSizedArray->arrayExpr ;
341+ auto arrayType = popRuntimeSizedArray->arrayType ;
342+ auto arrayMT = dynamic_cast <RuntimeSizedArrayT*>(arrayType->kind );
343+ assert (arrayMT);
344+ bool arrayKnownLive = true ;
345+
346+ auto arrayRef = translateExpression (globalState, functionState, blockState, builder, arrayExpr);
347+ globalState->getRegion (arrayType)
348+ ->checkValidReference (FL (), functionState, builder, arrayType, arrayRef);
349+
350+ auto arrayLenRef =
351+ globalState->getRegion (arrayType)
352+ ->getRuntimeSizedArrayLength (
353+ functionState, builder, arrayType, arrayRef, arrayKnownLive);
354+ auto arrayLenLE =
355+ globalState->getRegion (globalState->metalCache ->i32Ref )
356+ ->checkValidReference (FL (),
357+ functionState, builder, globalState->metalCache ->i32Ref , arrayLenRef);
358+ globalState->getRegion (globalState->metalCache ->i32Ref )
359+ ->checkValidReference (FL (),
360+ functionState, builder, globalState->metalCache ->i32Ref , arrayLenRef);
361+
362+ auto indexLE = LLVMBuildSub (builder, arrayLenLE, constI32LE (globalState, 1 ), " index" );
363+ auto indexRef =
364+ wrap (globalState->getRegion (globalState->metalCache ->i32Ref ), globalState->metalCache ->i32Ref , indexLE);
365+
366+ auto hasElementsLE = LLVMBuildICmp (builder, LLVMIntNE, arrayLenLE, constI32LE (globalState, 0 ), " hasElements" );
367+ buildIf (globalState, functionState, builder, hasElementsLE, [globalState](LLVMBuilderRef bodyBuilder) {
368+ buildPrint (globalState, bodyBuilder, " Error: Cannot pop element from empty runtime-sized array!" );
369+ });
370+
371+ auto resultRef =
372+ globalState->getRegion (arrayType)
373+ ->popRuntimeSizedArrayNoBoundsCheck (functionState, builder, arrayType, arrayMT, arrayRef, arrayKnownLive, indexRef);
374+
375+ globalState->getRegion (arrayType)
376+ ->dealias (
377+ AFL (" popRuntimeSizedArrayNoBoundsCheck" ), functionState, builder, arrayType, arrayRef);
378+
379+ return resultRef;
380+ } else if (auto dmrsa = dynamic_cast <DestroyMutRuntimeSizedArray*>(expr)) {
381+ buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
382+ auto arrayKind = dmrsa->arrayKind ;
383+ auto arrayExpr = dmrsa->arrayExpr ;
384+ auto arrayType = dmrsa->arrayType ;
385+ bool arrayKnownLive = true ;
386+
387+ auto arrayRef = translateExpression (globalState, functionState, blockState, builder, arrayExpr);
388+ globalState->getRegion (arrayType)
389+ ->checkValidReference (FL (), functionState, builder, arrayType, arrayRef);
390+ auto arrayLenRef =
391+ globalState->getRegion (arrayType)
392+ ->getRuntimeSizedArrayLength (
393+ functionState, builder, arrayType, arrayRef, arrayKnownLive);
394+ auto arrayLenLE =
395+ globalState->getRegion (globalState->metalCache ->i32Ref )
396+ ->checkValidReference (FL (),
397+ functionState, builder, globalState->metalCache ->i32Ref , arrayLenRef);
398+
399+ auto hasElementsLE = LLVMBuildICmp (builder, LLVMIntNE, arrayLenLE, constI32LE (globalState, 0 ), " hasElements" );
400+ buildIf (globalState, functionState, builder, hasElementsLE, [globalState](LLVMBuilderRef bodyBuilder) {
401+ buildPrint (globalState, bodyBuilder, " Error: Destroying non-empty array!" );
402+ });
403+
404+ if (arrayType->ownership == Ownership::OWN) {
405+ globalState->getRegion (arrayType)
406+ ->discardOwningRef (FL (), functionState, blockState, builder, arrayType, arrayRef);
407+ } else if (arrayType->ownership == Ownership::SHARE) {
408+ // We dont decrement anything here, we're only here because we already hit zero.
409+
410+ // Free it!
411+ globalState->getRegion (arrayType)
412+ ->deallocate (
413+ AFL (" DestroyRSAIntoF" ), functionState, builder, arrayType, arrayRef);
414+ } else {
415+ assert (false );
416+ }
417+
418+ return makeEmptyTupleRef (globalState);
419+ } else if (auto dirsa = dynamic_cast <DestroyImmRuntimeSizedArray*>(expr)) {
420+ buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
421+ auto consumerType = dirsa->consumerType ;
422+ auto arrayKind = dirsa->arrayKind ;
423+ auto arrayExpr = dirsa->arrayExpr ;
424+ auto consumerExpr = dirsa->consumerExpr ;
425+ auto consumerMethod = dirsa->consumerMethod ;
426+ auto arrayType = dirsa->arrayType ;
306427 bool arrayKnownLive = true ;
307428
308429 auto arrayRef = translateExpression (globalState, functionState, blockState, builder, arrayExpr);
@@ -331,7 +452,7 @@ Ref translateExpressionInner(
331452
332453 auto elementRef =
333454 globalState->getRegion (arrayType)
334- ->deinitializeElementFromRSA (
455+ ->popRuntimeSizedArrayNoBoundsCheck (
335456 functionState, bodyBuilder, arrayType, arrayKind, arrayRef, arrayKnownLive, indexRef);
336457 std::vector<Ref> argExprRefs = { consumerRef, elementRef };
337458
@@ -457,7 +578,7 @@ Ref translateExpressionInner(
457578 auto arrayKind = runtimeSizedArrayStore->arrayKind ;
458579 bool arrayKnownLive = runtimeSizedArrayStore->arrayKnownLive || globalState->opt ->overrideKnownLiveTrue ;
459580
460- auto elementType = globalState->program ->getRuntimeSizedArray (arrayKind)->rawArray -> elementType ;
581+ auto elementType = globalState->program ->getRuntimeSizedArray (arrayKind)->elementType ;
461582
462583 auto arrayRefLE = translateExpression (globalState, functionState, blockState, builder, arrayExpr);
463584 globalState->getRegion (arrayType)
@@ -533,9 +654,12 @@ Ref translateExpressionInner(
533654 } else if (auto newArrayFromValues = dynamic_cast <NewArrayFromValues*>(expr)) {
534655 buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
535656 return translateNewArrayFromValues (globalState, functionState, blockState, builder, newArrayFromValues);
536- } else if (auto constructRuntimeSizedArray = dynamic_cast <ConstructRuntimeSizedArray*>(expr)) {
657+ } else if (auto nirsa = dynamic_cast <NewImmRuntimeSizedArray*>(expr)) {
658+ buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
659+ return translateNewImmRuntimeSizedArray (globalState, functionState, blockState, builder, nirsa);
660+ } else if (auto nmrsa = dynamic_cast <NewMutRuntimeSizedArray*>(expr)) {
537661 buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
538- return translateConstructRuntimeSizedArray (globalState, functionState, blockState, builder, constructRuntimeSizedArray );
662+ return translateNewMutRuntimeSizedArray (globalState, functionState, blockState, builder, nmrsa );
539663 } else if (auto staticArrayFromCallable = dynamic_cast <StaticArrayFromCallable*>(expr)) {
540664 buildFlare (FL (), globalState, functionState, builder, typeid (*expr).name ());
541665 return translateStaticArrayFromCallable (globalState, functionState, blockState, builder, staticArrayFromCallable);
0 commit comments