Changeset 32 for branches/UltimatOS/UAssembler.pas
- Timestamp:
- Jul 10, 2022, 12:37:58 AM (2 years ago)
- Location:
- branches/UltimatOS
- Files:
-
- 1 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
branches/UltimatOS
- Property svn:ignore
-
old new 2 2 UltimatOS.lps 3 3 UltimatOS.res 4 UltimatOS.dbg 4 5 lib
-
- Property svn:ignore
-
branches/UltimatOS/UAssembler.pas
r31 r32 1 unit U InstructionWriter;1 unit UAssembler; 2 2 3 3 interface … … 25 25 end; 26 26 27 { TLabel } 28 27 29 TLabel = class 28 30 Name: string; 29 31 Address: TAddress; 30 32 ForwardRefs: array of TAddress; 33 procedure AddForwardRef(Address: TAddress); 31 34 end; 32 35 … … 50 53 end; 51 54 52 { T InstructionWriter }53 54 T InstructionWriter = class55 { TAssembler } 56 57 TAssembler = class 55 58 private 56 59 InstructionDefs: TInstructionDefs; … … 59 62 LineNumber: Integer; 60 63 function ParseText(var Text: string; Separator: Char): string; 64 function ParseConst(Text: string; out Value: Integer): Boolean; 65 function ParseLabel(Text: string; out Value: Integer): Boolean; 66 function ParseNumber(Text: string; out Value: Integer): Boolean; 61 67 procedure WriteParam(Param: TInstructionParam; var Text: string); 62 68 procedure Error(Text: string); … … 65 71 IP: Integer; 66 72 procedure Parse(Lines: TStrings); 67 procedure Write(Text: string);73 procedure ParseLine(Text: string); 68 74 procedure WriteInstruction(Instruction: TInstruction); 69 75 procedure WriteAddress(Address: TAddress); … … 80 86 implementation 81 87 88 { TLabel } 89 90 procedure TLabel.AddForwardRef(Address: TAddress); 91 begin 92 SetLength(ForwardRefs, Length(ForwardRefs) + 1); 93 ForwardRefs[Length(ForwardRefs) - 1] := Address; 94 end; 95 82 96 { TConstants } 83 97 … … 144 158 145 159 146 { T InstructionWriter }147 148 function T InstructionWriter.ParseText(var Text: string; Separator: Char160 { TAssembler } 161 162 function TAssembler.ParseText(var Text: string; Separator: Char 149 163 ): string; 150 164 var … … 161 175 end; 162 176 163 procedure TInstructionWriter.WriteParam(Param: TInstructionParam; var Text: string); 177 function TAssembler.ParseConst(Text: string; out Value: Integer): Boolean; 178 var 179 FoundConstant: TConstant; 180 begin 181 FoundConstant := Constants.SearchByName(UpperCase(Text)); 182 if Assigned(FoundConstant) then begin 183 Result := True; 184 Value := FoundConstant.Value; 185 end; 186 Result := False; 187 end; 188 189 function TAssembler.ParseLabel(Text: string; out Value: Integer): Boolean; 190 var 191 FoundLabel: TLabel; 192 begin 193 Result := True; 194 FoundLabel := Labels.SearchByName(UpperCase(Text)); 195 if Assigned(FoundLabel) then begin 196 // Existing label 197 if FoundLabel.Address = -1 then begin 198 FoundLabel.AddForwardRef(IP); 199 Value := 0; 200 end else 201 Value := FoundLabel.Address; 202 end else begin 203 // Forward label reference 204 with Labels.AddNew(UpperCase(Text), -1) do begin 205 AddForwardRef(IP); 206 end; 207 Value := 0; 208 end; 209 end; 210 211 function TAssembler.ParseNumber(Text: string; out Value: Integer): Boolean; 212 begin 213 if TryStrToInt(Text, Value) then begin 214 Result := True; 215 end else 216 Result := False; 217 end; 218 219 procedure TAssembler.WriteParam(Param: TInstructionParam; var Text: string); 164 220 var 165 221 Address: string; … … 179 235 Exit; 180 236 end; 181 FoundLabel := Labels.SearchByName(UpperCase(Address)); 182 if Assigned(FoundLabel) then begin 183 // Existing label 184 WriteAddress(FoundLabel.Address); 185 end else begin 186 // Forward label reference 187 with Labels.AddNew(UpperCase(Address), -1) do begin 188 SetLength(ForwardRefs, Length(ForwardRefs) + 1); 189 ForwardRefs[Length(ForwardRefs) - 1] := IP; 190 end; 191 WriteAddress(0); 192 end; 237 ParseLabel(Address, Value); 238 WriteAddress(Value); 193 239 end; 194 240 end; … … 205 251 Exit; 206 252 end; 207 FoundLabel := Labels.SearchByName(UpperCase(Address)); 208 if Assigned(FoundLabel) then begin 209 // Existing label 210 WriteAddress(FoundLabel.Address); 211 end else begin 212 // Forward label reference 213 with Labels.AddNew(UpperCase(Address), -1) do begin 214 SetLength(ForwardRefs, Length(ForwardRefs) + 1); 215 ForwardRefs[Length(ForwardRefs) - 1] := IP; 216 end; 217 WriteAddress(0); 218 end; 253 ParseLabel(Address, Value); 254 WriteAddress(Value); 219 255 end; 220 256 end else Error('Expected indirect address ' + Address); … … 245 281 Exit; 246 282 end; 247 FoundLabel := Labels.SearchByName(UpperCase(Address)); 248 if Assigned(FoundLabel) then begin 249 // Existing label 250 WriteData(FoundLabel.Address); 251 end else begin 252 // Forward label reference 253 with Labels.AddNew(UpperCase(Address), -1) do begin 254 SetLength(ForwardRefs, Length(ForwardRefs) + 1); 255 ForwardRefs[Length(ForwardRefs) - 1] := IP; 256 end; 257 WriteData(0); 258 end; 283 ParseLabel(Address, Value); 284 WriteData(Value); 259 285 end; 260 286 end; … … 273 299 end; 274 300 275 procedure T InstructionWriter.Error(Text: string);301 procedure TAssembler.Error(Text: string); 276 302 begin 277 303 raise Exception.Create(IntToStr(LineNumber) + ': ' + Text); 278 304 end; 279 305 280 procedure T InstructionWriter.Parse(Lines: TStrings);306 procedure TAssembler.Parse(Lines: TStrings); 281 307 var 282 308 I: Integer; … … 284 310 for I := 0 to Lines.Count - 1 do begin 285 311 LineNumber := I + 1; 286 Write(Lines[I]);312 ParseLine(Lines[I]); 287 313 end; 288 314 for I := 0 to Labels.Count - 1 do begin … … 292 318 end; 293 319 294 procedure T InstructionWriter.Write(Text: string);320 procedure TAssembler.ParseLine(Text: string); 295 321 var 296 322 FoundLabel: TLabel; … … 304 330 Lines: TStringList; 305 331 Param: string; 332 Num: Integer; 306 333 begin 307 334 // Remove comments … … 375 402 if InstructionName = 'dd' then begin 376 403 while Text <> '' do begin 377 Param := ParseText(Text, ',');404 Param := Trim(ParseText(Text, ',')); 378 405 if Param.StartsWith('"') and Param.EndsWith('"') then begin 379 406 Param := Copy(Param, 2, Length(Param) - 2); 380 407 for I := 1 to Length(Param) do 381 408 WriteCardinal(Ord(Param[I])); 382 end else WriteCardinal(StrToInt(Param)); 383 end; 409 end else begin 410 if ParseNumber(Param, Num) then 411 WriteCardinal(Num) 412 else if ParseConst(Param, Num) then 413 WriteCardinal(Num) 414 else if ParseLabel(Param, Num) then 415 WriteCardinal(Num); 416 end; 417 end; 418 end else 419 if InstructionName = 'org' then begin 420 IP := StrToInt(Text); 384 421 end else Error('Unsupported directive name ' + InstructionName); 385 422 end else begin … … 396 433 end; 397 434 398 procedure T InstructionWriter.WriteInstruction(Instruction: TInstruction);435 procedure TAssembler.WriteInstruction(Instruction: TInstruction); 399 436 begin 400 437 Memory.Data[IP] := Byte(Instruction); … … 402 439 end; 403 440 404 procedure T InstructionWriter.WriteAddress(Address: TAddress);441 procedure TAssembler.WriteAddress(Address: TAddress); 405 442 begin 406 443 PAddress(@Memory.Data[IP])^ := Address; … … 408 445 end; 409 446 410 procedure T InstructionWriter.WriteData(Data: TData);447 procedure TAssembler.WriteData(Data: TData); 411 448 begin 412 449 PData(@Memory.Data[IP])^ := Data; … … 414 451 end; 415 452 416 procedure T InstructionWriter.WriteIndex(Index: Byte);453 procedure TAssembler.WriteIndex(Index: Byte); 417 454 begin 418 455 Memory.Data[IP] := Index; … … 420 457 end; 421 458 422 procedure T InstructionWriter.WriteReg(Index: Byte);459 procedure TAssembler.WriteReg(Index: Byte); 423 460 begin 424 461 Memory.Data[IP] := Index; … … 426 463 end; 427 464 428 procedure T InstructionWriter.WriteByte(Value: Byte);465 procedure TAssembler.WriteByte(Value: Byte); 429 466 begin 430 467 Memory.Data[IP] := Value; … … 432 469 end; 433 470 434 procedure T InstructionWriter.WriteWord(Value: Word);471 procedure TAssembler.WriteWord(Value: Word); 435 472 begin 436 473 PWord(@Memory.Data[IP])^ := Value; … … 438 475 end; 439 476 440 procedure T InstructionWriter.WriteCardinal(Value: Cardinal);477 procedure TAssembler.WriteCardinal(Value: Cardinal); 441 478 begin 442 479 PCardinal(@Memory.Data[IP])^ := Value; … … 444 481 end; 445 482 446 constructor T InstructionWriter.Create;483 constructor TAssembler.Create; 447 484 begin 448 485 Labels := TLabels.Create; … … 477 514 AddNew(inOr, 'OR', ipRegIndex, ipRegIndex); 478 515 AddNew(inXor, 'XOR', ipRegIndex, ipRegIndex); 479 end; 480 end; 481 482 destructor TInstructionWriter.Destroy; 516 AddNew(inInt, 'INT', ipIndex); 517 AddNew(inReti, 'RETI'); 518 AddNew(inEnableInt, 'EI'); 519 AddNew(inDisableInt, 'DI'); 520 end; 521 end; 522 523 destructor TAssembler.Destroy; 483 524 begin 484 525 FreeAndNil(InstructionDefs);
Note:
See TracChangeset
for help on using the changeset viewer.