Ignore:
Timestamp:
Oct 20, 2010, 1:35:59 PM (9 years ago)
Author:
mulligan
Message:

New features:

1) conditional jumps to labels implemented

[Note: all conditional jumps are to relative addresses;

if the label is too far away, an assert false is currently raised]

2) we now have a preamble of declarations of data labels in external

memory and a new pseudo-instruction "Mov" to place the value of a
data label into DPTR.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • Deliverables/D4.1/ASMInterpret.ml

    r199 r200  
    881881module IntMap = Map.Make(struct type t = int let compare = compare end);;
    882882
    883 let assembly l =
     883
     884let assembly_jump addr_of =
     885 function
     886    `JC a1 -> `JC (addr_of a1)
     887  | `JNC a1 -> `JNC (addr_of a1)
     888  | `JB (a1,a2) -> `JB (a1,addr_of a2)
     889  | `JNB (a1,a2) -> `JNB (a1,addr_of a2)
     890  | `JBC (a1,a2) -> `JBC (a1,addr_of a2)
     891  | `JZ a1 -> `JZ (addr_of a1)
     892  | `JNZ a1 -> `JNZ (addr_of a1)
     893  | `CJNE (a1,a2) -> `CJNE (a1,addr_of a2)
     894  | `DJNZ (a1,a2) -> `DJNZ (a1,addr_of a2)
     895;;
     896
     897let assembly (preamble,l) =
     898 let datalabels,_ =
     899  List.fold_left
     900   (fun (datalabels,addr) (name,size) ->
     901     let addr16 = vect_of_int addr `Sixteen in
     902      StringMap.add name addr16 datalabels, addr+size
     903   ) (StringMap.empty,0) preamble
     904 in
    884905 let pc,labels,costs =
    885906  List.fold_left
     
    888909        `Label s -> pc, StringMap.add s pc labels, costs
    889910      | `Cost s -> pc, labels, IntMap.add pc s costs
     911      | `Mov (_,_) -> pc, labels, costs
    890912      | `Jmp _
    891913      | `Call _ -> pc + 3, labels, costs  (*CSC: very stupid: always expand to worst opcode *)
     914      | `WithLabel i ->
     915          let fake_addr _ = `REL (zero `Eight) in
     916          let fake_jump = assembly_jump fake_addr i in
     917          let i',pc',_ = fetch (load_code_memory (assembly1 fake_jump)) (vect_of_int 0 `Sixteen) in
     918           assert (fake_jump = i');
     919           (pc + int_of_vect pc',labels, costs)
    892920      | #instruction as i ->
    893921        let i',pc',_ = fetch (load_code_memory (assembly1 i)) (vect_of_int 0 `Sixteen) in
     
    903931            `Label _
    904932          | `Cost _ -> []
     933          | `WithLabel i ->
     934              let addr_of (`Label s) =
     935               let addr = StringMap.find s labels in
     936               (* NOT IMPLEMENTED YET; NEEDS SMART ALGORITHM *)
     937                assert (addr < 256);
     938                `REL (vect_of_int addr `Eight)
     939              in
     940               assembly1 (assembly_jump addr_of i)
     941          | `Mov (`DPTR,s) ->
     942              let addrr16 = StringMap.find s datalabels in
     943               assembly1 (`MOV (`U4 (`DPTR,`DATA16 addrr16)))
    905944          | `Jmp s ->
    906945              let pc_offset = StringMap.find s labels in
Note: See TracChangeset for help on using the changeset viewer.