Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eca9e18

Browse files
committed
Only catch locally thrown exceptions in cppia
1 parent f6a0ae2 commit eca9e18

6 files changed

Lines changed: 122 additions & 77 deletions

File tree

src/hx/cppia/ArrayBuiltin.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,24 +1686,20 @@ static int SLJIT_CALL arrayPushString( ArrayAnyImpl *inArray, String *inVal)
16861686

16871687
static int SLJIT_CALL arraySetInt( ArrayAnyImpl *inArray, int inIndex, int inVal)
16881688
{
1689-
*(int *)0=0;
16901689
inArray->set(inIndex,inVal);
16911690
return inVal;
16921691
}
16931692
static hx::Object * SLJIT_CALL arraySetObject( ArrayAnyImpl *inArray, int inIndex, hx::Object *inVal)
16941693
{
1695-
*(int *)0=0;
16961694
inArray->set(inIndex,Dynamic(inVal));
16971695
return inVal;
16981696
}
16991697
static void SLJIT_CALL arraySetFloat( ArrayAnyImpl *inArray, int inIndex, double *inVal)
17001698
{
1701-
*(int *)0=0;
17021699
inArray->set(inIndex,*inVal);
17031700
}
17041701
static void SLJIT_CALL arraySetString( ArrayAnyImpl *inArray, int inIndex, String *inVal)
17051702
{
1706-
*(int *)0=0;
17071703
inArray->set(inIndex,*inVal);
17081704
}
17091705

src/hx/cppia/Cppia.cpp

Lines changed: 74 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ CppiaExpr *convertToFunction(CppiaExpr *inExpr);
231231

232232

233233

234-
235234
bool TypeData::isClassOf(Dynamic inInstance)
236235
{
237236
if (cppiaClass)
@@ -1261,18 +1260,29 @@ struct CastExpr : public CppiaDynamicExpr
12611260
{
12621261
if (destType==etObject)
12631262
value->genCode(compiler, inDest, destType);
1263+
else if (destType==etString)
1264+
{
1265+
value->genCode(compiler, inDest, etString);
1266+
}
12641267
else
12651268
{
1266-
value->genCode(compiler, sJitTemp0, etObject);
1267-
compiler->convert(sJitTemp0,etObject, inDest, destType);
1269+
JitTemp o(compiler,jtPointer);
1270+
value->genCode(compiler, o, etObject);
1271+
compiler->convert(o,etObject, inDest, destType);
12681272
}
12691273
}
12701274
else
12711275
{
12721276
value->genCode(compiler, sJitArg0, etObject);
1277+
JumpId notNull = compiler->compare(cmpP_NOT_ZERO, sJitArg0.as(jtPointer), (void *)0);
1278+
compiler->returnNull(inDest, destType);
1279+
JumpId returnedNull = compiler->jump();
1280+
1281+
compiler->comeFrom(notNull);
12731282
compiler->callNative( (void *)callDynamicToArrayType, sJitArg0.as(jtPointer), (int)arrayType );
12741283
compiler->checkException();
12751284
compiler->convertReturnReg(etObject, inDest, destType);
1285+
compiler->comeFrom(returnedNull);
12761286
}
12771287
}
12781288
#endif
@@ -1758,6 +1768,13 @@ struct CallHaxe : public CppiaExpr
17581768
}
17591769

17601770
#ifdef CPPIA_JIT
1771+
static void SLJIT_CALL tryCallHaxe( void (SLJIT_CALL *func)(StackContext *), StackContext *ctx )
1772+
{
1773+
TRY_NATIVE
1774+
func(ctx);
1775+
CATCH_NATIVE
1776+
}
1777+
17611778
void genCode(CppiaCompiler *compiler, const JitVal &inDest,ExprType destType)
17621779
{
17631780
int framePos = compiler->getCurrentFrameSize();
@@ -1798,8 +1815,7 @@ struct CallHaxe : public CppiaExpr
17981815
// Store new frame in context ...
17991816
compiler->add( sJitCtxFrame, sJitFrame, JitVal(framePos) );
18001817

1801-
// Compiled yet
1802-
compiler->call( JitVal( (void *)(function.execute)), sJitCtx );
1818+
compiler->callNative( (void *)tryCallHaxe, JitVal( (void *)(function.execute)), sJitCtx );
18031819

18041820
// TODO - from signature
18051821
bool isBoolReturn = false;
@@ -2842,7 +2858,7 @@ struct GetFieldByName : public CppiaDynamicExpr
28422858
{
28432859
// TODO - interfaces
28442860
if (object)
2845-
object->genCode(compiler, sJitTemp0, etObject);
2861+
object->genCode(compiler, sJitTemp0.as(jtPointer), etObject);
28462862
else if (isStatic)
28472863
{
28482864
compiler->move(sJitTemp0, (void *)&staticClass.mPtr);
@@ -2851,38 +2867,38 @@ struct GetFieldByName : public CppiaDynamicExpr
28512867
else
28522868
{
28532869
// this...
2854-
compiler->move(sJitTemp0, JitFramePos(0,etObject) );
2870+
compiler->move(sJitTemp0, sJitThis );
28552871
}
28562872

28572873
switch(destType)
28582874
{
28592875
case etInt:
2860-
compiler->callNative( (void *)getFieldInt, sJitTemp0, (void *)&name);
2876+
compiler->callNative( (void *)getFieldInt, sJitTemp0.as(jtPointer), (void *)&name);
28612877
compiler->checkException();
2862-
compiler->move(inDest, sJitTemp0);
2878+
compiler->move(inDest.as(jtInt), sJitReturnReg.as(jtInt));
28632879
break;
28642880
case etFloat:
28652881
if (isMemoryVal(inDest))
28662882
{
2867-
compiler->callNative( (void *)getFieldFloat, sJitTemp0, (void *)&name, inDest.as(jtFloat) );
2883+
compiler->callNative( (void *)getFieldFloat, sJitTemp0.as(jtPointer), (void *)&name, inDest.as(jtFloat) );
28682884
compiler->checkException();
28692885
}
28702886
else
28712887
{
28722888
JitTemp floatResult(compiler, jtFloat);
2873-
compiler->callNative( (void *)getFieldFloat, sJitTemp0, &name, floatResult );
2889+
compiler->callNative( (void *)getFieldFloat, sJitTemp0.as(jtPointer), &name, floatResult );
28742890
compiler->checkException();
28752891
compiler->move(inDest.as(jtFloat), floatResult);
28762892
}
28772893
break;
28782894
case etString:
2879-
compiler->callNative( (void *)getFieldString, sJitTemp0, (void *)&name, inDest );
2895+
compiler->callNative( (void *)getFieldString, sJitTemp0.as(jtPointer), (void *)&name, inDest.as(jtString) );
28802896
compiler->checkException();
28812897
break;
28822898
case etObject:
2883-
compiler->callNative( (void *)getFieldObject, sJitTemp0, (void *)&name);
2899+
compiler->callNative( (void *)getFieldObject, sJitTemp0.as(jtPointer), (void *)&name);
28842900
compiler->checkException();
2885-
compiler->move(inDest.as(jtPointer), sJitTemp0.as(jtPointer));
2901+
compiler->move(inDest.as(jtPointer), sJitReturnReg.as(jtPointer));
28862902
break;
28872903
default: ;
28882904
}
@@ -3329,22 +3345,22 @@ struct MemReference : public CppiaExpr
33293345
if (REFMODE==locAbsolute)
33303346
{
33313347
compiler->move( sJitTemp0, JitVal( (void *)pointer ) );
3332-
compiler->convert( sJitTemp0.star(jtPointer,0), getType(),inDest, destType );
3348+
compiler->convert( sJitTemp0.star(jtPointer,0), getType(),inDest, destType, isBoolInt() );
33333349
}
33343350
else if (REFMODE==locObj)
33353351
{
33363352
object->genCode( compiler, sJitTemp2, etObject );
3337-
compiler->convert( sJitTemp2.star(jtPointer,offset) ,getType(),inDest, destType );
3353+
compiler->convert( sJitTemp2.star(jtPointer,offset) ,getType(),inDest, destType, isBoolInt() );
33383354
}
33393355
else if (REFMODE==locThis)
33403356
{
33413357
JitThisPos target(offset, getJitType(getType()) );
3342-
compiler->convert( target,getType(),inDest, destType );
3358+
compiler->convert( target,getType(),inDest, destType, isBoolInt() );
33433359
}
33443360
else
33453361
{
33463362
JitFramePos target(offset, getJitType(getType()));
3347-
compiler->convert( target,getType(),inDest, destType );
3363+
compiler->convert( target,getType(),inDest, destType, isBoolInt() );
33483364
}
33493365
}
33503366
#endif
@@ -3923,15 +3939,35 @@ struct MemReferenceCrement : public CppiaExpr
39233939
}
39243940
else if (op==coPostInc || op==coPostDec)
39253941
{
3926-
compiler->move( sJitTemp1.as(jtInt), ioPtr );
3927-
compiler->add( ioPtr, sJitTemp1.as(jtInt), diff );
3928-
compiler->convert( sJitTemp1, etInt, inDest, destType );
3942+
if (destType==etInt)
3943+
{
3944+
compiler->move( sJitTemp0.as(jtInt), ioPtr );
3945+
compiler->add( ioPtr, sJitTemp0.as(jtInt), diff );
3946+
compiler->move( inDest, sJitTemp0.as(jtInt) );
3947+
}
3948+
else
3949+
{
3950+
JitTemp result(compiler, jtInt);
3951+
compiler->move( result, ioPtr );
3952+
compiler->add( ioPtr, ioPtr, diff );
3953+
compiler->convert( result, etInt, inDest, destType );
3954+
}
39293955
}
39303956
else
39313957
{
3932-
compiler->add( sJitTemp1.as(jtInt), ioPtr, diff );
3933-
compiler->move( ioPtr, sJitTemp1.as(jtInt));
3934-
compiler->convert( sJitTemp1, etInt, inDest, destType );
3958+
if (destType==etInt)
3959+
{
3960+
compiler->add( sJitTemp1.as(jtInt), ioPtr, diff );
3961+
compiler->move( ioPtr, sJitTemp1.as(jtInt));
3962+
compiler->move( inDest, sJitTemp1.as(jtInt));
3963+
}
3964+
else
3965+
{
3966+
JitTemp result(compiler, jtInt);
3967+
compiler->add( result, ioPtr, diff );
3968+
compiler->move( ioPtr, result );
3969+
compiler->convert(result, etInt, inDest, destType);
3970+
}
39353971
}
39363972
break;
39373973

@@ -5832,17 +5868,20 @@ struct TryExpr : public CppiaVoidExpr
58325868

58335869
void genCode(CppiaCompiler *compiler, const JitVal &inDest, ExprType destType)
58345870
{
5835-
compiler->pushCatching();
5871+
ThrowList thisThrown;
5872+
ThrowList *oldList = compiler->pushCatching(&thisThrown);
58365873
body->genCode(compiler, inDest, destType);
5837-
compiler->popCatching();
58385874

5839-
if (compiler->hasThrown())
5875+
compiler->popCatching(oldList);
5876+
5877+
if (thisThrown.size()>0)
58405878
{
58415879
std::vector<JumpId> handledExceptions;
58425880

58435881
JumpId noThrow = compiler->jump();
58445882

5845-
compiler->catchThrown();
5883+
for(int i=0;i<thisThrown.size();i++)
5884+
compiler->comeFrom(thisThrown[i]);
58465885

58475886
for(int i=0;i<catches.size();i++)
58485887
{
@@ -6973,7 +7012,7 @@ struct OpAdd : public BinOp
69737012
JitTemp temp(compiler,jtFloat);
69747013
left->genCode(compiler,temp,etFloat);
69757014
right->genCode(compiler,sJitTempF0,etFloat);
6976-
if (destType==etObject)
7015+
if (destType==etObject || destType==etString)
69777016
{
69787017
compiler->add( sJitTempF0, temp, sJitTempF0 );
69797018
compiler->convert(sJitTempF0, etFloat, inDest, destType);
@@ -7164,7 +7203,9 @@ struct OpCompare : public OpCompareBase
71647203

71657204
static int SLJIT_CALL dynamicCompare(hx::Object *o0, hx::Object *o1)
71667205
{
7167-
return Dynamic(o0).Compare( Dynamic(o1) );
7206+
COMPARE compare;
7207+
7208+
return compare.test( Dynamic(o0), Dynamic(o1) );
71687209
}
71697210

71707211

@@ -7204,15 +7245,14 @@ struct OpCompare : public OpCompareBase
72047245
JitTemp lhs(compiler,jtPointer);
72057246
left->genCode(compiler, lhs, etObject);
72067247
right->genCode(compiler, sJitArg1, etObject);
7207-
72087248
compiler->callNative( (void *)dynamicCompare, lhs, sJitArg1.as(jtPointer) );
7209-
return compiler->compare( (JitCompare)(inReverse ? COMPARE::reverse :COMPARE::compare),
7210-
sJitReturnReg, (int) 0, inLabel );
7249+
return compiler->compare( inReverse ? cmpI_EQUAL : cmpI_NOT_EQUAL, sJitReturnReg.as(jtInt), (int) 0, inLabel );
72117250
}
72127251

72137252

72147253
default:
7215-
printf("todo - other compares\n");
7254+
left->genCode(compiler, sJitTemp0, etObject);
7255+
right->genCode(compiler, sJitTemp0, etObject);
72167256
}
72177257
return 0;
72187258
}

src/hx/cppia/CppiaCompiler.cpp

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ static void SLJIT_CALL intToStr(int inVal, String *outString)
5959
}
6060
static void SLJIT_CALL objToStr(hx::Object *inVal, String *outString)
6161
{
62-
*outString = inVal ? inVal->toString() : HX_CSTRING("null");
62+
*outString = inVal ? inVal->toString() : String();
6363
}
6464
int SLJIT_CALL objToInt(hx::Object *inVal)
6565
{
@@ -205,9 +205,9 @@ class CppiaJitCompiler : public CppiaCompiler
205205
struct sljit_compiler *compiler;
206206

207207
QuickVec<JumpId> allBreaks;
208-
QuickVec<JumpId> throwTargets;
208+
QuickVec<JumpId> uncaught;
209209
LabelId continuePos;
210-
int catchCount;
210+
ThrowList *catching;
211211

212212
bool usesCtx;
213213
bool usesThis;
@@ -237,7 +237,7 @@ class CppiaJitCompiler : public CppiaCompiler
237237
usesCtx = false;
238238
makesNativeCalls = false;
239239
continuePos = 0;
240-
catchCount = 0;
240+
catching = 0;
241241
maxFrameSize = frameSize = baseFrameSize = sizeof(void *) + inFrameSize;
242242
}
243243

@@ -302,14 +302,15 @@ class CppiaJitCompiler : public CppiaCompiler
302302
move( sJitThis, JitFramePos(0) );
303303

304304
frameSize = baseFrameSize;
305-
throwTargets.setSize(0);
305+
uncaught.setSize(0);
306+
catching = 0;
306307
}
307308

308309
CppiaFunc finishGeneration()
309310
{
310-
for(int i=0;i<throwTargets.size();i++)
311-
comeFrom(throwTargets[i]);
312-
throwTargets.setSize(0);
311+
for(int i=0;i<uncaught.size();i++)
312+
comeFrom(uncaught[i]);
313+
uncaught.setSize(0);
313314
sljit_emit_return(compiler, SLJIT_UNUSED, SLJIT_UNUSED, 0);
314315
CppiaFunc func = (CppiaFunc)sljit_generate_code(compiler);
315316
sljit_free_compiler(compiler);
@@ -698,6 +699,7 @@ class CppiaJitCompiler : public CppiaCompiler
698699
// May required indirect offsets
699700
void move(const JitVal &inDest, const JitVal &inSrc)
700701
{
702+
// TODO - better equality test
701703
if (inDest==inSrc || !inDest.valid())
702704
return;
703705

@@ -829,7 +831,6 @@ class CppiaJitCompiler : public CppiaCompiler
829831
case etInt:
830832
if (asBool)
831833
{
832-
trace("int->string");
833834
JumpId isFalse = compare( cmpI_ZERO, inSrc.as(jtInt), 0, 0);
834835
move(inTarget.as(jtInt), 4);
835836
move(inTarget.as(jtPointer)+4, (void *)String(true).__s );
@@ -1437,31 +1438,36 @@ class CppiaJitCompiler : public CppiaCompiler
14371438
localSize = restoreLocal;
14381439
}
14391440

1440-
bool hasThrown()
1441-
{
1442-
return !throwTargets.empty();
1443-
}
14441441
void checkException()
14451442
{
1446-
throwTargets.push( compare( cmpP_NOT_ZERO,sJitCtx.star(jtPointer, offsetof(hx::StackContext,exception)),(void *)0, 0 ) );
1443+
JumpId onException = compare( cmpP_NOT_ZERO,sJitCtx.star(jtPointer, offsetof(hx::StackContext,exception)),(void *)0, 0 );
1444+
if (catching)
1445+
catching->push_back( onException );
1446+
else
1447+
uncaught.push( onException );
1448+
14471449
}
14481450

14491451

1450-
void catchThrown()
1452+
void addThrow()
14511453
{
1452-
for(int i=0;i<throwTargets.size();i++)
1453-
comeFrom(throwTargets[i]);
1454-
throwTargets.setSize(0);
1454+
if (catching)
1455+
catching->push_back( jump() );
1456+
else
1457+
uncaught.push( jump() );
14551458
}
14561459

1457-
void addThrow()
1460+
ThrowList *pushCatching(ThrowList *inList)
1461+
{
1462+
ThrowList *oldList = catching;
1463+
catching = inList;
1464+
return oldList;
1465+
}
1466+
void popCatching(ThrowList *inList)
14581467
{
1459-
throwTargets.push( jump() );
1468+
catching = inList;
14601469
}
14611470

1462-
void pushCatching() { catchCount++; }
1463-
void popCatching() { catchCount--; }
1464-
bool hasCatching() { return catchCount; }
14651471
};
14661472

14671473
void CppiaCompiler::freeCompiled(CppiaFunc inFunc)

0 commit comments

Comments
 (0)