summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--codegen.hs28
-rw-r--r--simple.nl1
2 files changed, 27 insertions, 2 deletions
diff --git a/codegen.hs b/codegen.hs
index 6a51dff..bf47a62 100644
--- a/codegen.hs
+++ b/codegen.hs
@@ -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
diff --git a/simple.nl b/simple.nl
index a09feb6..fd6c5c7 100644
--- a/simple.nl
+++ b/simple.nl
@@ -7,4 +7,5 @@ int main(i32 argc, ptr(ptr(i8)) argv) {
//int i = g_var;
//int a = i + 2;
int i;
+ 1;
}