Print numbers with multiple digits
This commit is contained in:
parent
8eaeb5afa3
commit
f9c1f8d892
@ -35,6 +35,7 @@ common warnings
|
|||||||
library elna-internal
|
library elna-internal
|
||||||
import: warnings
|
import: warnings
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
|
Language.Elna.Allocator
|
||||||
Language.Elna.Architecture.RiscV
|
Language.Elna.Architecture.RiscV
|
||||||
Language.Elna.AST
|
Language.Elna.AST
|
||||||
Language.Elna.CodeGenerator
|
Language.Elna.CodeGenerator
|
||||||
|
3
lib/Language/Elna/Allocator.hs
Normal file
3
lib/Language/Elna/Allocator.hs
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
module Language.Elna.Allocator
|
||||||
|
(
|
||||||
|
) where
|
@ -10,7 +10,7 @@ import Data.Vector (Vector)
|
|||||||
import qualified Data.Vector as Vector
|
import qualified Data.Vector as Vector
|
||||||
import qualified Data.Text.Encoding as Text.Encoding
|
import qualified Data.Text.Encoding as Text.Encoding
|
||||||
import Language.Elna.Location (Identifier(..))
|
import Language.Elna.Location (Identifier(..))
|
||||||
import Language.Elna.Intermediate (Quadruple(..))
|
import Language.Elna.Intermediate (Operand(..), Quadruple(..))
|
||||||
import qualified Language.Elna.Architecture.RiscV as RiscV
|
import qualified Language.Elna.Architecture.RiscV as RiscV
|
||||||
import Language.Elna.SymbolTable (SymbolTable)
|
import Language.Elna.SymbolTable (SymbolTable)
|
||||||
|
|
||||||
@ -34,17 +34,20 @@ generateCode _ = HashMap.foldlWithKey' go Vector.empty
|
|||||||
|
|
||||||
quadruple :: Quadruple -> Vector Statement
|
quadruple :: Quadruple -> Vector Statement
|
||||||
quadruple StartQuadruple = Vector.fromList
|
quadruple StartQuadruple = Vector.fromList
|
||||||
[ Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.SP RiscV.ADDI RiscV.SP (negate 4))
|
[ Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.SP RiscV.ADDI RiscV.SP (negate 8))
|
||||||
, Instruction (RiscV.BaseInstruction RiscV.Store $ RiscV.S 0 RiscV.SW RiscV.SP RiscV.S0)
|
, Instruction (RiscV.BaseInstruction RiscV.Store $ RiscV.S 4 RiscV.SW RiscV.SP RiscV.S0)
|
||||||
, Instruction (RiscV.BaseInstruction RiscV.Store $ RiscV.S 4 RiscV.SW RiscV.SP RiscV.RA)
|
, Instruction (RiscV.BaseInstruction RiscV.Store $ RiscV.S 8 RiscV.SW RiscV.SP RiscV.RA)
|
||||||
, Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.S0 RiscV.ADDI RiscV.SP 4)
|
, Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.S0 RiscV.ADDI RiscV.SP 8)
|
||||||
|
]
|
||||||
|
quadruple (ParameterQuadruple (IntOperand intValue)) = Vector.fromList
|
||||||
|
[ Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.A0 RiscV.ADDI RiscV.Zero $ fromIntegral intValue)
|
||||||
|
, Instruction (RiscV.BaseInstruction RiscV.Store $ RiscV.S 0 RiscV.SW RiscV.SP RiscV.A0)
|
||||||
]
|
]
|
||||||
quadruple (ParameterQuadruple _) = mempty
|
|
||||||
quadruple (CallQuadruple callName _) = Vector.singleton
|
quadruple (CallQuadruple callName _) = Vector.singleton
|
||||||
$ Instruction (RiscV.CallInstruction callName)
|
$ Instruction (RiscV.CallInstruction callName)
|
||||||
quadruple StopQuadruple = Vector.fromList
|
quadruple StopQuadruple = Vector.fromList
|
||||||
[ Instruction (RiscV.BaseInstruction RiscV.Load $ RiscV.I RiscV.S0 RiscV.LW RiscV.SP 0)
|
[ Instruction (RiscV.BaseInstruction RiscV.Load $ RiscV.I RiscV.S0 RiscV.LW RiscV.SP 4)
|
||||||
, Instruction (RiscV.BaseInstruction RiscV.Load $ RiscV.I RiscV.RA RiscV.LW RiscV.SP 4)
|
, Instruction (RiscV.BaseInstruction RiscV.Load $ RiscV.I RiscV.RA RiscV.LW RiscV.SP 8)
|
||||||
, Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.SP RiscV.ADDI RiscV.SP 4)
|
, Instruction (RiscV.BaseInstruction RiscV.OpImm $ RiscV.I RiscV.SP RiscV.ADDI RiscV.SP 8)
|
||||||
, Instruction (RiscV.BaseInstruction RiscV.Jalr $ RiscV.I RiscV.RA RiscV.JALR RiscV.Zero 0)
|
, Instruction (RiscV.BaseInstruction RiscV.Jalr $ RiscV.I RiscV.RA RiscV.JALR RiscV.Zero 0)
|
||||||
]
|
]
|
||||||
|
1
tests/expectations/print_2_digits.txt
Normal file
1
tests/expectations/print_2_digits.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
13
|
3
tests/vm/print_2_digits.elna
Normal file
3
tests/vm/print_2_digits.elna
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
proc main() {
|
||||||
|
printi(13);
|
||||||
|
}
|
@ -6,29 +6,41 @@
|
|||||||
|
|
||||||
.text
|
.text
|
||||||
printi:
|
printi:
|
||||||
addi sp, sp, -4
|
addi sp, sp, -16
|
||||||
sw s0, 0(sp)
|
sw s0, 0(sp)
|
||||||
sw ra, 4(sp)
|
sw ra, 4(sp)
|
||||||
addi s0, sp, 4
|
addi s0, sp, 16
|
||||||
|
|
||||||
|
addi t1, zero, '\n'
|
||||||
|
sb t1, -1(s0)
|
||||||
|
|
||||||
|
# t0 - Whole number.
|
||||||
|
# t1 - Constant 10.
|
||||||
|
# a1 - Local buffer.
|
||||||
|
# t2 - Current character.
|
||||||
|
lw t0, 0(s0)
|
||||||
|
addi t1, zero, 10
|
||||||
|
addi a1, s0, -2
|
||||||
|
|
||||||
|
.digit10:
|
||||||
|
rem t2, t0, t1
|
||||||
|
addi t2, t2, '0'
|
||||||
|
sb t2, 0(a1)
|
||||||
|
div t0, t0, t1
|
||||||
|
addi a1, a1, -1
|
||||||
|
bne zero, t0, .digit10
|
||||||
|
|
||||||
addi t0, a0, 0
|
|
||||||
addi a0, a0, '0'
|
|
||||||
sw a0, 0(s0)
|
|
||||||
addi a0, x0, 1
|
|
||||||
addi a1, s0, 0
|
|
||||||
addi a2, x0, 1
|
|
||||||
addi a7, x0, 64
|
|
||||||
ecall
|
ecall
|
||||||
|
|
||||||
addi t1, x0, '\n'
|
addi a0, zero, 1
|
||||||
sw t1, 0(s0)
|
addi a1, a1, 1
|
||||||
|
sub a2, s0, a1
|
||||||
|
addi a7, zero, 64
|
||||||
ecall
|
ecall
|
||||||
|
|
||||||
addi a0, t0, 0
|
|
||||||
|
|
||||||
lw s0, 0(sp)
|
lw s0, 0(sp)
|
||||||
lw ra, 4(sp)
|
lw ra, 4(sp)
|
||||||
addi sp, sp, 4
|
addi sp, sp, 16
|
||||||
ret
|
ret
|
||||||
|
|
||||||
_start:
|
_start:
|
||||||
|
Loading…
Reference in New Issue
Block a user