1
This commit is contained in:
parent
1bbc80de29
commit
858db4dc0e
4 changed files with 109 additions and 29 deletions
17
ass3/zbirnik/lib/drugiPrehod.ml
Normal file
17
ass3/zbirnik/lib/drugiPrehod.ml
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
(*funkcija drugega prehoda*)
|
||||
open Simtab
|
||||
open SemanticAnalyzer
|
||||
|
||||
let drugiPrehod (ifl : lineSemantic list) (simtab : symtab) =
|
||||
let prviUkaz, ostaliUkazi =
|
||||
match ifl with
|
||||
| [] -> failwith "empty code"
|
||||
| x :: xs -> x, xs
|
||||
in
|
||||
(*if Opcode == start then*)
|
||||
let _ =
|
||||
if prviUkaz.opcode = START then
|
||||
(*TODO write listing line*)
|
||||
in
|
||||
(*TODO write header record to object program*)
|
||||
(*TODO initialize first text record*)
|
||||
|
|
@ -1,10 +1,16 @@
|
|||
(*tukaj je vse povezano s prvim prehodom zbirnika*)
|
||||
open SemanticAnalyzer
|
||||
open Simtab
|
||||
|
||||
let startAddr = ref 0
|
||||
let locctr = ref 0
|
||||
|
||||
let prviPrehod (code : lineSemantic list) =
|
||||
|
||||
(*funkcija prvega prehoda, !!! za END ne sme biti nič več definiranih ukazov ali direktiv!*)
|
||||
let prviPrehod (code : lineSemantic list) : int*lineSemantic list*symtab=
|
||||
(*inicializiramo startAddr, loctr in simtab*)
|
||||
let startAddr = ref 0 in
|
||||
let locctr = ref 0 in
|
||||
let simtab = create_symtab () in
|
||||
|
||||
let prviUkaz, ostaliUkazi =
|
||||
match code with
|
||||
| [] -> failwith "empty code"
|
||||
|
|
@ -12,34 +18,47 @@ let prviPrehod (code : lineSemantic list) =
|
|||
in
|
||||
|
||||
(*if Opcode == start then*)
|
||||
|
||||
if prviUkaz.opcode = START then
|
||||
let stAdr = get_string_from_mnemType prviUkaz.mnem in
|
||||
let stAdr = match stAdr with | Some s -> s | None -> "0" in
|
||||
startAddr := (int_of_string stAdr);
|
||||
locctr := !startAddr;
|
||||
(*TODO write line to intermediate file*)
|
||||
else
|
||||
(*initialize locctr to 0*)
|
||||
locctr := 0
|
||||
|
||||
;
|
||||
let intermediateFileList =
|
||||
if prviUkaz.opcode = START then
|
||||
let stAdr = get_string_from_mnemType prviUkaz.mnem in
|
||||
let stAdr = match stAdr with | Some s -> s | None -> "0" in
|
||||
startAddr := (int_of_string stAdr);
|
||||
locctr := !startAddr;
|
||||
(*write line to intermediate file*)
|
||||
prviUkaz.loc <- Some !locctr;
|
||||
[prviUkaz]
|
||||
else
|
||||
(*initialize locctr to 0*)
|
||||
(locctr := 0;
|
||||
[])
|
||||
in
|
||||
(*while opcode != END loop*)
|
||||
let rec loop ostaliUkazi =
|
||||
let rec loop ostaliUkazi prgLen intermediateFileList =
|
||||
match ostaliUkazi with
|
||||
| [] -> () (*smo konec*)
|
||||
| x :: _ when x.opcode = END -> () (*konec while loopa*) (*TODO write last line to intermediate file, save the locctr - st addr as prg len*)
|
||||
| x :: xs -> match x.opcode with
|
||||
| COMMENT -> loop xs (*if this is a comment line*)
|
||||
| [] -> prgLen, (List.rev intermediateFileList), simtab (*smo konec*)
|
||||
| x :: _ when x.opcode = END -> (*konec while loopa*) (*write last line to intermediate file, save the locctr - st addr as prg len*)
|
||||
x.loc <- Some !locctr;
|
||||
let intermediateFileList = x :: intermediateFileList in
|
||||
let prgLen = !locctr - !startAddr in
|
||||
prgLen, (List.rev intermediateFileList), simtab (*vrnemo program length*)
|
||||
| x :: xs -> match x.opcode with (*loopamo*)
|
||||
| COMMENT -> loop xs prgLen intermediateFileList(*if this is a comment line*)
|
||||
| _ ->
|
||||
(*if there is a symmbol in label field*)
|
||||
let s = match x.label with
|
||||
begin
|
||||
match x.label with
|
||||
| None -> ()
|
||||
| Some s -> () (*search simtab for s ...*)
|
||||
in
|
||||
| Some s ->
|
||||
(*search simtab for s ...*)
|
||||
(*doda simbol ce še ne obstaja, sier faila*)
|
||||
add_symbol simtab s !locctr
|
||||
end;
|
||||
|
||||
(*write line to intermediate file*)
|
||||
x.loc <- Some !locctr;
|
||||
(*search opcode -> smo ze naredili v semanticni analizi, tu bomo le ustrezno povečali locctr*)
|
||||
locctr := !locctr + x.len;
|
||||
(*TODO write line to intermediate file*)
|
||||
loop xs (*read nex input line end while*)
|
||||
in loop ostaliUkazi
|
||||
let intermediateFileList = x :: intermediateFileList in
|
||||
loop xs prgLen (intermediateFileList) (*read nex input line end while*)
|
||||
in loop ostaliUkazi 0 intermediateFileList
|
||||
|
||||
|
|
|
|||
|
|
@ -277,13 +277,45 @@ let getMnemType (opcode : mnemonic) (ext : bool) (operand : string list) : mnemo
|
|||
| _, _ -> failwith (Printf.sprintf "Invalid operands for opcode %s: %d operands"
|
||||
(string_of_mnemonic opcode) (List.length operand))
|
||||
|
||||
let getLen (mnem : mnemonic) (ext : bool) (mnemType : mnemonic_type) : int =
|
||||
42 (*TODO*)
|
||||
let getLen (mnemType : mnemonic_type) (mnem : mnemonic) : int =
|
||||
match mnemType with
|
||||
(* ----------- directives with no memory ----------- *)
|
||||
| MnemonicD
|
||||
| MnemonicDn _
|
||||
| COMMENT -> 0
|
||||
|
||||
(* ----------- instruction formats ----------- *)
|
||||
| MnemonicF1 -> 1
|
||||
| MnemonicF2n _
|
||||
| MnemonicF2r _
|
||||
| MnemonicF2rn _
|
||||
| MnemonicF2rr _ -> 2
|
||||
|
||||
| MnemonicF3
|
||||
| MnemonicF3m _ -> 3
|
||||
|
||||
| MnemonicF4m _ -> 4
|
||||
|
||||
(* ----------- storage directives ----------- *)
|
||||
| MnemonicSn n ->
|
||||
begin
|
||||
match mnem with
|
||||
| RESB -> n
|
||||
| RESW -> 3*n
|
||||
| _ -> failwith "Narobe mnemonic glede na mnemType" (* RESB *)
|
||||
end
|
||||
| MnemonicSd _ ->
|
||||
begin
|
||||
match mnem with
|
||||
| BYTE -> 1
|
||||
| WORD -> 3
|
||||
| _ -> failwith "Narobe mnemoic glede na mnemType"
|
||||
end
|
||||
|
||||
let checkLineSemantic (line : line) : lineSemantic =
|
||||
let mnem, ext = getMnem line.opcode in
|
||||
let mnemType = getMnemType mnem ext line.operand in
|
||||
let dolzina = getLen mnem ext mnemType in
|
||||
let dolzina = getLen mnemType mnem in
|
||||
{line_no = line.line_no; label = line.label; ext = ext; opcode = mnem; mnem = mnemType; comment = line.comment; len = dolzina; loc = line.loc}
|
||||
|
||||
let checkLineSemanticOfCode (code : line list) : lineSemantic list =
|
||||
|
|
|
|||
12
ass3/zbirnik/lib/simtab.ml
Normal file
12
ass3/zbirnik/lib/simtab.ml
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
(*simbolna tabela*)
|
||||
|
||||
type symtab = (string, int) Hashtbl.t
|
||||
|
||||
let create_symtab () : symtab =
|
||||
Hashtbl.create 64
|
||||
|
||||
let add_symbol (tab : symtab) (label : string) (addr : int) =
|
||||
if Hashtbl.mem tab label then
|
||||
failwith "Duplicate symbol in simtab"
|
||||
else
|
||||
Hashtbl.add tab label addr
|
||||
Loading…
Add table
Add a link
Reference in a new issue