diff --git a/README.markdown b/README.markdown index 2dc7bae..ccbf08c 100644 --- a/README.markdown +++ b/README.markdown @@ -219,12 +219,15 @@ No duplicate declarations. | routine main { | lda screen | tax + | stx score | tay + | sty score | cmp score | ldx score | txa | ldy score | tya + | sta screen | } = .org 0 = .word $0801 @@ -236,10 +239,13 @@ No duplicate declarations. = main: = lda screen = tax + = stx score = tay + = sty score = cmp score = ldx score = txa = ldy score = tya + = sta screen = rts diff --git a/src/SixtyPical/Analyzer.hs b/src/SixtyPical/Analyzer.hs index 64a1d28..9efe5bf 100644 --- a/src/SixtyPical/Analyzer.hs +++ b/src/SixtyPical/Analyzer.hs @@ -9,7 +9,7 @@ import SixtyPical.Model -- -- -- -- data-flow-analysis context -- -- -- -- data Usage = Unknown - | Value StorageLocation -- obviously a bit daft for now + | Value DataValue -- obviously a bit daft for now | Retained StorageLocation deriving (Show, Ord, Eq) @@ -62,8 +62,8 @@ checkRoutine (Routine name (instr : instrs)) progCtx routCtx = in checkRoutine (Routine name instrs) progCtx routCtx' -checkInstr (LOAD reg addr) progCtx routCtx = - Map.insert reg (Value addr) routCtx +checkInstr (LOADIMM reg imm) progCtx routCtx = + Map.insert reg (Value imm) routCtx checkInstr (COPY src dst) progCtx routCtx = Map.insert dst (Map.findWithDefault Unknown src routCtx) routCtx checkInstr (JSR name) progCtx routCtx = diff --git a/src/SixtyPical/Checker.hs b/src/SixtyPical/Checker.hs index 1353a20..664b113 100644 --- a/src/SixtyPical/Checker.hs +++ b/src/SixtyPical/Checker.hs @@ -29,7 +29,9 @@ blockUsedLocations [] = [] blockUsedLocations (instr:instrs) = (instrUsedLocations instr) ++ blockUsedLocations instrs -instrUsedLocations (LOAD reg (NamedLocation loc)) = [loc] +--instrUsedLocations (LOADIMM reg (NamedLocation loc)) = [loc] +instrUsedLocations (COPY (NamedLocation loc) _) = [loc] +instrUsedLocations (COPY _ (NamedLocation loc)) = [loc] instrUsedLocations (CMP reg (NamedLocation loc)) = [loc] -- TODO: JSR... instrUsedLocations (IFEQ b1 b2) = diff --git a/src/SixtyPical/Emitter.hs b/src/SixtyPical/Emitter.hs index c08e87c..3254a7a 100644 --- a/src/SixtyPical/Emitter.hs +++ b/src/SixtyPical/Emitter.hs @@ -35,9 +35,18 @@ emitInstrs _ _ [] = "" emitInstrs p r (instr:instrs) = " " ++ emitInstr p r instr ++ "\n" ++ emitInstrs p r instrs -emitInstr p r (LOAD A (NamedLocation label)) = "lda " ++ label -emitInstr p r (LOAD X (NamedLocation label)) = "ldx " ++ label -emitInstr p r (LOAD Y (NamedLocation label)) = "ldy " ++ label +emitInstr p r (LOADIMM A val) = "lda #" ++ (show val) +emitInstr p r (LOADIMM X val) = "ldx #" ++ (show val) +emitInstr p r (LOADIMM Y val) = "ldy #" ++ (show val) + +emitInstr p r (COPY A (NamedLocation label)) = "sta " ++ label +emitInstr p r (COPY X (NamedLocation label)) = "stx " ++ label +emitInstr p r (COPY Y (NamedLocation label)) = "sty " ++ label +emitInstr p r (COPY (NamedLocation label) A) = "lda " ++ label +emitInstr p r (COPY (NamedLocation label) X) = "ldx " ++ label +emitInstr p r (COPY (NamedLocation label) Y) = "ldy " ++ label + + emitInstr p r (CMP A (NamedLocation label)) = "cmp " ++ label emitInstr p r (COPY A X) = "tax" diff --git a/src/SixtyPical/Model.hs b/src/SixtyPical/Model.hs index 6d98e71..4b1b5f8 100644 --- a/src/SixtyPical/Model.hs +++ b/src/SixtyPical/Model.hs @@ -4,6 +4,7 @@ module SixtyPical.Model where -- -- -- -- machine model -- -- -- -- +type DataValue = Int -- LET'S ASSUME THIS IS AT LEAST 8 BITS type Address = Int -- LET'S ASSUME THIS IS AT LEAST 16 BITS type LocationName = String @@ -45,7 +46,7 @@ data Decl = Assign LocationName Size Address -- .alias type RoutineName = String -data Instruction = LOAD StorageLocation StorageLocation +data Instruction = LOADIMM StorageLocation DataValue | COPY StorageLocation StorageLocation | CMP StorageLocation StorageLocation | JSR RoutineName diff --git a/src/SixtyPical/Parser.hs b/src/SixtyPical/Parser.hs index 4881cb6..5b464dd 100644 --- a/src/SixtyPical/Parser.hs +++ b/src/SixtyPical/Parser.hs @@ -74,6 +74,7 @@ block = do command :: Parser Instruction command = cmp <|> (try lda) <|> (try ldx) <|> (try ldy) <|> + (try sta) <|> (try stx) <|> (try sty) <|> (try txa) <|> (try tax) <|> (try tya) <|> (try tay) <|> beq <|> nop @@ -95,21 +96,42 @@ lda = do string "lda" spaces l <- locationName - return (LOAD A (NamedLocation l)) + return (COPY (NamedLocation l) A) ldx :: Parser Instruction ldx = do string "ldx" spaces l <- locationName - return (LOAD X (NamedLocation l)) + return (COPY (NamedLocation l) X) ldy :: Parser Instruction ldy = do string "ldy" spaces l <- locationName - return (LOAD Y (NamedLocation l)) + return (COPY (NamedLocation l) Y) + +sta :: Parser Instruction +sta = do + string "sta" + spaces + l <- locationName + return (COPY A (NamedLocation l)) + +stx :: Parser Instruction +stx = do + string "stx" + spaces + l <- locationName + return (COPY X (NamedLocation l)) + +sty :: Parser Instruction +sty = do + string "sty" + spaces + l <- locationName + return (COPY Y (NamedLocation l)) txa :: Parser Instruction txa = do