diff options
Diffstat (limited to 'BuildIR.hs')
-rw-r--r-- | BuildIR.hs | 66 |
1 files changed, 38 insertions, 28 deletions
@@ -258,21 +258,20 @@ convertExpression (EBin bo e1 e2 _) nextnext = do switchBlock bl3 ref <- genTemp (sizeof $ fromJust $ retTypeBO bo (fromJust $ typeof e1) (fromJust $ typeof e2)) case bo of - BOAdd -> addIns $ IAri AAdd ref1 ref2 - BOSub -> addIns $ IAri ASub ref1 ref2 - BOMul -> addIns $ IAri AMul ref1 ref2 - BODiv -> addIns $ IAri ADiv ref1 ref2 - BOMod -> addIns $ IAri AMod ref1 ref2 - BOEq -> addIns $ IAri AEq ref1 ref2 - BONeq -> addIns $ IAri ANeq ref1 ref2 - BOGt -> addIns $ IAri AGt ref1 ref2 - BOLt -> addIns $ IAri ALt ref1 ref2 - BOGeq -> addIns $ IAri AGeq ref1 ref2 - BOLeq -> addIns $ IAri ALeq ref1 ref2 + BOAdd -> addIns $ IAri AAdd ref ref1 ref2 + BOSub -> addIns $ IAri ASub ref ref1 ref2 + BOMul -> addIns $ IAri AMul ref ref1 ref2 + BODiv -> addIns $ IAri ADiv ref ref1 ref2 + BOMod -> addIns $ IAri AMod ref ref1 ref2 + BOEq -> addIns $ IAri AEq ref ref1 ref2 + BONeq -> addIns $ IAri ANeq ref ref1 ref2 + BOGt -> addIns $ IAri AGt ref ref1 ref2 + BOLt -> addIns $ IAri ALt ref ref1 ref2 + BOGeq -> addIns $ IAri AGeq ref ref1 ref2 + BOLeq -> addIns $ IAri ALeq ref ref1 ref2 BOPow -> error $ "Pow operator not implemented" BOAnd -> undefined BOOr -> undefined - addIns $ IMov ref ref1 setTerm $ IJmp nextnext return ref convertExpression (EUn UONot e mt) nextnext = @@ -287,11 +286,14 @@ convertExpression (ESubscript arr sub t) nextnext = do bl3 <- newBlockNoSwitch subref <- convertExpression sub bl3 switchBlock bl3 - addIns $ IAri AMul subref (Constant (refSize subref) (fromIntegral elemsz)) - addIns $ IAri AAdd subref (Constant (refSize subref) (fromIntegral $ sizeof TInt)) - addIns $ IAri AAdd arrref subref + offref <- genTemp (refSize subref) + off8ref <- genTemp (refSize subref) + elemptr <- genTemp (refSize arrref) + addIns $ IAri AMul offref subref (Constant (refSize subref) (fromIntegral elemsz)) + addIns $ IAri AAdd off8ref offref (Constant (refSize subref) (fromIntegral $ sizeof TInt)) + addIns $ IAri AAdd elemptr arrref off8ref ref <- genTemp elemsz - addIns $ ILoad ref arrref + addIns $ ILoad ref elemptr setTerm $ IJmp nextnext return ref convertExpression (ECast dt e) nextnext = do @@ -314,9 +316,11 @@ convertExpression (ENew t sze) nextnext = do szref <- convertExpression sze bl2 switchBlock bl2 ref <- genTemp (sizeof $ TArr t Nothing) - addIns $ IAri AMul szref (Constant (sizeof TInt) (fromIntegral $ sizeof t)) - addIns $ IAri AAdd szref (Constant (sizeof TInt) (fromIntegral $ sizeof TInt)) - addIns $ ICallr ref "_builtin_malloc" [szref] + argref' <- genTemp (sizeof TInt) + argref <- genTemp (sizeof TInt) + addIns $ IAri AMul argref' szref (Constant (sizeof TInt) (fromIntegral $ sizeof t)) + addIns $ IAri AAdd argref argref' (Constant (sizeof TInt) (fromIntegral $ sizeof TInt)) + addIns $ ICallr ref "_builtin_malloc" [argref] setTerm $ IJmp nextnext return ref @@ -333,13 +337,16 @@ convertAsExpression (AESubscript ae2 expr mrt) valueref nextnext = do let elemsz = sizeof $ fromJust mrt ae2ref <- goLoad ae2 bl2 <- newBlockNoSwitch - offref <- convertExpression expr bl2 + subref <- convertExpression expr bl2 switchBlock bl2 + offref' <- genTemp (sizeof TInt) + offref <- genTemp (sizeof TInt) + elemptr <- genTemp (sizeof TInt) -- TODO: do bounds checking - addIns $ IAri AMul offref (Constant (sizeof TInt) (fromIntegral elemsz)) - addIns $ IAri AAdd offref (Constant (sizeof TInt) (fromIntegral $ sizeof TInt)) - addIns $ IAri AAdd ae2ref offref - addIns $ IStore ae2ref valueref + addIns $ IAri AMul offref' subref (Constant (sizeof TInt) (fromIntegral elemsz)) + addIns $ IAri AAdd offref offref' (Constant (sizeof TInt) (fromIntegral $ sizeof TInt)) + addIns $ IAri AAdd elemptr ae2ref offref + addIns $ IStore elemptr valueref setTerm $ IJmp nextnext where goLoad :: AsExpression -> BuildM Ref @@ -358,10 +365,13 @@ convertAsExpression (AESubscript ae2 expr mrt) valueref nextnext = do bl2 <- newBlockNoSwitch eref <- convertExpression expr' bl2 switchBlock bl2 + offref' <- genTemp (sizeof TInt) + offref <- genTemp (sizeof TInt) + elemptr <- genTemp (sizeof TInt) -- TODO: do bounds checking - addIns $ IAri AMul eref (Constant (sizeof TInt) (fromIntegral elemsz)) - addIns $ IAri AAdd eref (Constant (sizeof TInt) (fromIntegral $ sizeof TInt)) - addIns $ IAri AAdd ref eref + addIns $ IAri AMul offref' eref (Constant (sizeof TInt) (fromIntegral elemsz)) + addIns $ IAri AAdd offref offref' (Constant (sizeof TInt) (fromIntegral $ sizeof TInt)) + addIns $ IAri AAdd elemptr ref offref dstref <- genTemp elemsz - addIns $ ILoad dstref ref + addIns $ ILoad dstref elemptr return dstref |