diff options
author | tomsmeding <tom.smeding@gmail.com> | 2017-01-24 21:52:56 +0100 |
---|---|---|
committer | tomsmeding <tom.smeding@gmail.com> | 2017-01-24 21:52:56 +0100 |
commit | 935f59da631ec97f6c7a0b49caae127e7a9aaa78 (patch) | |
tree | 1d6453abf0eb7417e64e40d1142c4b0d51d921d2 | |
parent | f9f666c7088ad2acde6dad3bd57ebf8438ba9ea1 (diff) |
Codegen single literal int
-rw-r--r-- | codegen.hs | 28 | ||||
-rw-r--r-- | simple.nl | 1 |
2 files changed, 27 insertions, 2 deletions
@@ -71,8 +71,16 @@ changeBlock name = state $ \s -> ((), s {currentBlock = Just name}) addInstr :: A.Instruction -> CGMonad LLName addInstr instr = do name <- getNewName "t" - let append (A.BasicBlock n il t) = A.BasicBlock n (il ++ [A.Name name A.:= instr]) t + addNamedInstr $ A.Name name A.:= instr + +addNamedInstr :: A.Named A.Instruction -> CGMonad LLName +addNamedInstr instr@(A.Name name A.:= _) = do + let append (A.BasicBlock n il t) = A.BasicBlock n (il ++ [instr]) t state $ \s -> (name, s {allBlocks = Map.adjust append (fromJust (currentBlock s)) (allBlocks s)}) +addNamedInstr _ = undefined + +addNamedInstrList :: [A.Named A.Instruction] -> CGMonad LLName +addNamedInstrList l = mapM addNamedInstr l >>= return . last setTerminator :: A.Terminator -> CGMonad () setTerminator term = do @@ -87,6 +95,11 @@ lookupVar :: LLName -> CGMonad LLName lookupVar name = liftM (fromJust . Map.lookup name . variables) get +namedName :: A.Named a -> LLName +namedName (A.Name name A.:= _) = name +namedName _ = undefined + + codegen :: Program -- Program to compile -> String -- Module name -> String -- File name of source @@ -166,7 +179,10 @@ genSingle :: Statement -> CGMonad LLName -- name of first BasicBlock genSingle StEmpty following = newBlockJump following genSingle (StBlock block) following = genBlock block following -genSingle (StExpr expr) following = undefined +genSingle (StExpr expr) following = do + bb <- newBlockJump following + void $ genExpression expr >>= addNamedInstrList + return bb genSingle (StVarDeclaration t n Nothing) following = do bb <- newBlockJump following label <- addInstr $ A.Alloca (toLLVMType t) Nothing 0 [] @@ -174,6 +190,14 @@ genSingle (StVarDeclaration t n Nothing) following = do return bb genSingle (StVarDeclaration _ _ (Just _)) _ = undefined +genExpression :: Expression -> CGMonad [A.Named A.Instruction] +genExpression (ExLit (LitInt i) (Just t@(TypeInt sz))) = do + aname <- getNewName "t" + rname <- getNewName "t" + return [A.Name aname A.:= A.Alloca (toLLVMType t) Nothing 0 [] + ,A.Name rname A.:= A.Store False (A.LocalReference (toLLVMType t) (A.Name aname)) + (A.ConstantOperand (A.C.Int (fromIntegral sz) i)) Nothing 0 []] + toLLVMType :: Type -> A.Type @@ -7,4 +7,5 @@ int main(i32 argc, ptr(ptr(i8)) argv) { //int i = g_var; //int a = i + 2; int i; + 1; } |