Fix bug in SNaN & QNaN translation.
[marekmrva_bc.git] / AsmFileIOClass.pas
blobc9c570d28e722c5a21b83d71d994b3db50573b5d
1 unit AsmFileIOClass;
3 interface
5 uses
6 ConstantsClass, FunctionsClass, HardwareClass, PrefixTreeClass,
7 ResourcesClass, SteppingClass, TypesClass,
8 SysUtils;
10 type
12 { TAsmFileIO }
14 TAsmFileIO = class
15 private
16 pError: String;
17 function pErrorSet: Boolean; virtual;
18 procedure pSetError(Input: String = GLOB_NO_ERROR; FileName: String = ''; Line: PInteger = nil); virtual;
19 public
20 function LoadFromFile(const FileName: string; Step: TStepping): Boolean; virtual;
21 function SaveToFile(const FileName: string; Step: TStepping): Boolean; virtual;
22 property LastError: String read pError write pError;
23 end;
25 implementation
27 // ************************************************************************** //
28 // * TAsmFileIO implementation * //
29 // ************************************************************************** //
31 function TAsmFileIO.pErrorSet: Boolean;
32 begin
33 Result := not(pError = GLOB_NO_ERROR);
34 end;
36 procedure TAsmFileIO.pSetError(Input: String = GLOB_NO_ERROR;
37 FileName: String = ''; Line: PInteger = nil);
38 begin
39 pError := Input;
40 if not(FileName = '') then
41 if not(Line = nil) then
42 pError := FileName + '(' + IntToStr(Line^) + '): ' + pError
43 else
44 pError := FileName + ': ' + pError;
45 if not(pError = GLOB_NO_ERROR) then LogWrite(pError, True);
46 end;
48 function TAsmFileIO.LoadFromFile(const FileName: string; Step: TStepping
49 ): Boolean;
50 var
51 i, lstate: Integer;
52 ldata, linst: TLines;
53 lfile: TextFile;
54 lhardware: THardware;
55 lline: String;
56 lsystem: TLine;
57 begin
58 pSetError;
59 Result := False;
60 SetLength(ldata, 0);
61 SetLength(linst, 0);
62 LogSystemTime;
63 LogWrite(FIO_LOADING + FileName);
64 try
65 AssignFile(lfile, FileName);
66 FileMode := fmOpenRead;
67 Reset(lfile);
68 lstate := 0;
69 i := 0;
70 while not Eof(lfile) do
71 begin
72 Readln(lfile, lline);
73 i := i + 1;
74 if ContainsCharacter(lline, FIO_COMMENT) then
75 begin
76 LogWrite(FIO_COMMENT + ParseAfterFirst(lline, FIO_COMMENT));
77 lline := ParseBeforeFirst(lline, FIO_COMMENT);
78 end;
79 lline := OmmitEverywhere(TrimCharacter(lline, ' '), ' ', ' ');
80 lline := UpperCase(lline);
81 if (lline = '') then Continue;
82 case lstate of
84 begin
85 if not IsPrefixOf(FIO_SYSTEM, lline) then
86 begin
87 pSetError(FIO_SYSTEM_NOT_FOUND, FileName);
88 LogWrite(FIO_ERROR_LOADING + FileName);
89 Exit;
90 end;
91 lsystem := ToTLine(lline, i);
92 lstate := lstate + 1;
93 end;
95 if (lline = FIO_DATA) then lstate := lstate + 1
96 else lstate := lstate + 2;
98 if (lline = FIO_CODE) then lstate := lstate + 1
99 else ldata := MergeTLinesLine(ldata, ToTLine(lline, i));
101 linst := MergeTLinesLine(linst, ToTLine(lline, i));
102 end;
103 end;
104 finally
105 CloseFile(lfile);
106 end;
107 lhardware := THardware.Create;
109 with lhardware do
110 begin
111 for i := 0 to (Length(ldata) - 1) do
112 if not ValidateOperand(ldata[i].Line) then
113 pSetError(LastError, FileName, @ldata[i].Number);
114 if pErrorSet then
115 begin
116 LogWrite(FIO_ERROR_LOADING);
117 Exit;
118 end;
119 for i := 0 to (Length(ldata) - 1) do
120 OperandAdd(ldata[i].Line);
121 for i := 0 to (Length(linst) - 1) do
122 if not ValidateInstruction(linst[i].Line) then
123 pSetError(LastError, FileName, @linst[i].Number);
124 if pErrorSet then
125 begin
126 LogWrite(FIO_ERROR_LOADING);
127 Exit;
128 end;
129 end;
130 finally
131 lhardware.Free;
132 end;
133 with Step.Hardware do
134 begin
135 InitializeOperands;
136 for i := 0 to (Length(ldata) - 1) do
137 OperandAdd(ldata[i].Line);
138 Step.InitializeBlocks;
139 for i := 0 to (Length(linst) - 1) do
140 Step.AddStepBlock(STEP_LAST, InstructionByName(linst[i].Line));
141 end;
142 Result := True;
143 LogWrite(FIO_LOADED);
144 end;
146 function TAsmFileIO.SaveToFile(const FileName: string; Step: TStepping
147 ): Boolean;
149 i, j: Integer;
150 lfile: TextFile;
151 loper: POperandRecord;
152 ltrees: TPrefixTrees;
153 begin
154 pSetError;
155 Result := True;
156 LogSystemTime;
157 LogWrite(FIO_SAVING + FileName);
159 AssignFile(lfile, FileName);
160 Rewrite(lfile);
161 Writeln(lfile, FIO_SYSTEM + ' ' + APP_VERSION);
162 Writeln(lfile);
163 Writeln(lfile, FIO_DATA);
164 ltrees := Step.Hardware.Operands.GetAllDescendants;
165 for i := 0 to (Length(ltrees) - 1) do
166 begin
167 loper := POperandRecord(ltrees[i].Data);
168 if (loper^.Default = '') then Continue;
169 for j := 0 to (Length(sOperandTypes) - 1) do
170 with sOperandTypes[j] do
171 if (OperandType = loper^.OperandType) then
172 begin
173 Writeln(lfile, ' ' + loper^.Name + OPER_SEP + Description);
174 Break;
175 end;
176 end;
177 Writeln(lfile);
178 Writeln(lfile, FIO_CODE);
179 for i := 0 to (Step.Length - 1) do
180 Writeln(lfile, ' ' + Step[i].CallFunction.Name);
181 finally
182 CloseFile(lfile);
183 end;
184 LogWrite(FIO_SAVED);
185 end;
187 end.