SPO_JakaFurlan/ass3/zbirnik/lib/drugiPrehod.ml
2025-12-13 16:11:31 +01:00

55 lines
No EOL
2.6 KiB
OCaml

(*funkcija drugega prehoda*)
open Simtab
open SemanticAnalyzer
open Encoder
let drugiPrehod (ifl : lineSemantic list) (simtab : symtab) (lenProg : int) : string list =
let prviUkaz, ostaliUkazi =
match ifl with
| [] -> failwith "empty code"
| x :: xs -> x, xs
in
(*if Opcode == start then*)
let objectCodeList = (*string list ki hrani vrstice objektne kode*)
if prviUkaz.opcode = START then
getZaglavje prviUkaz lenProg
else
failwith "prvi ukaz ni START"
in
let instructionList : string list = [] in (*opcodeList shranjuje kode ukazov pretvorjene v stringe, ko se napolni se appenda k objectCodeList*)
let rec loop (ostaliUkazi : lineSemantic list) (objectCodeList : string list) (startAddr : int) (instructionList : string list) : string list =
match ostaliUkazi with
| [] -> () (*smo konec*)
| x :: _ when x.opcode = END -> () (*TODO write last text record to object programm*)
(*TODO write end record to object program*)
(*TODOwrite last listing line*)
| x :: xs ->
(*pridobi začeten naslov TEXT vrstice, če je le ta prazna*)
let newStartAddress = match instructionList with
| [] -> begin match x.loc with None -> failwith "ukaz nima lokacije!" | Some l -> l end
| _ -> startAddr
in
match x.opcode with
| COMMENT -> loop xs objectCodeList newStartAddress instructionList
| RESB | RESW -> (*dodamo trenutno TEXT vrstico k objektni kodi in inicializiramo novo*)
let newTextLine = instructionList_to_objectCode (List.rev instructionList) newStartAddress in (*naredimo novo T vrstico*)
begin
match instructionList with (*če je instructionList prazen, npr. 2 zaporedna RESB ukaza, ne dodamo te prazne T vrstice v obj kodo*)
| [] -> loop xs objectCodeList newStartAddress []
| ntl -> loop xs (ntl :: objectCodeList) newStartAddress []
end
| _ -> (*if not comment or reserve*)
let opcode = getOpcode x.opcode in
(*if simbol na mestu opeanda*)
let isSymbol, symbol = operand_is_symbol x.mnem in
if isSymbol then
let locSimbola = find_symbol tab symbol in
let opcode = create_opcode x.opcode locSimbola in
(*TODO dodaj opcode v line, ce ni prostora dodaj line v file in naredi novega*)
()
else
let opcode = create_opcode x.opcode (int_of_string symbol) in (*symbol je tukaj kar stevilska vrednost/register*)
(*TODO dodaj opcode v line, ce ni prostora dodaj line v file in naredi novega*)
()
in loop ostaliUkazi objectCodeList startAddr instructionList