-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
SiViC_ASM_Assembler.pas
1252 lines (1131 loc) · 47.6 KB
/
SiViC_ASM_Assembler.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit SiViC_ASM_Assembler;
{$INCLUDE '.\SiViC_defs.inc'}
interface
uses
Classes,
AuxTypes,
SiViC_Common,
SiViC_Instructions,
SiViC_Program,
SiViC_ASM_Lists,
SiViC_ASM_Parser_Base,
SiViC_ASM_Parser_Instr,
SiViC_ASM_Parser;
const
SVC_ASM_ASSEMBLER_LOWORD_SUFFIX = '_LO';
SVC_ASM_ASSEMBLER_HIWORD_SUFFIX = '_HI';
SVC_ASM_ASSEMBLER_LENGTH_SUFFIX = '_LEN';
type
TSVCAssemblerItem_Sys = record
Identifier: String;
Value: TSVCComp;
Counter: Integer;
SrcLineIdx: Integer;
end;
TSVCAssemblerList_Sys = record
Arr: array of TSVCAssemblerItem_Sys;
Count: Integer;
end;
TSVCAssemblerItem_Const = record
Identifier: String;
Value: TSVCNative;
Size: TSVCValueSize;
SrcLineIdx: Integer;
SrcLinePos: Integer;
end;
TSVCAssemblerList_Const = record
Arr: array of TSVCAssemblerItem_Const;
Count: Integer;
end;
TSVCAssemblerItem_Var = record
Identifier: String;
Size: TSVCNative;
Data: TSVCByteArray;
Referencing: Boolean;
ReferenceIdentifier: String;
ReferenceIdentifierPos: Integer;
ReferenceOffset: TSVCNative;
Address: TSVCNative;
SrcLineIdx: Integer;
SrcLinePos: Integer;
end;
TSVCAssemblerList_Var = record
Arr: array of TSVCAssemblerItem_Var;
Count: Integer;
end;
TSVCAssemblerItem_Label = record
Identifier: String;
Address: TSVCNative;
SrcLineIdx: Integer;
end;
TSVCAssemblerList_Label = record
Arr: array of TSVCAssemblerItem_Label;
Count: Integer;
end;
TSVCAssemblerItem_Unresolved = record
Identifier: String;
Count: Integer;
SrcLineIdx: Integer;
SrcLinePos: Integer;
end;
TSVCAssemblerList_Unresolved = record
Arr: array of TSVCAssemblerItem_Unresolved;
Count: Integer;
end;
TSVCAssemblerLineType = (altSys,altConst,altVar,altLabel,altData,altInstr,altOther);
TSVCAssemblerInstruction = record
Address: TSVCNative;
Data: TSVCByteArray;
Replacements: array of TSVCParserResult_Instr_Replacement;
end;
TSVCAssemblerLine = record
Str: String;
SrcLineIdx: Integer;
LineType: TSVCAssemblerLineType;
Instruction: TSVCAssemblerInstruction;
end;
TSVCAssemblerLines = record
Arr: array of TSVCAssemblerLine;
Count: Integer;
end;
TSVCAssemblerMessage = record
MessageType: TSVCParserMessageType;
Text: String;
LineIdx: Integer;
Position: Integer;
end;
TSVCAssemblerMessages = record
Arr: array of TSVCAssemblerMessage;
Count: Integer;
Counters: array[TSVCParserMessageType] of Integer;
end;
TSVCAssembler = class(TObject)
private
fLists: TSVCListManager;
fParser: TSVCParser;
fSystemValues: TSVCAssemblerList_Sys;
fConstants: TSVCAssemblerList_Const;
fVariables: TSVCAssemblerList_Var;
fLabels: TSVCAssemblerList_Label;
fUnresolved: TSVCAssemblerList_Unresolved;
fAssemblerLines: TSVCAssemblerLines;
fAssemblerMessages: TSVCAssemblerMessages;
// assembling variables
fParsedLine: String;
fParsedLineIdx: Integer;
fParsedLinePos: Integer;
fCodeSize: TMemSize;
fMemTaken: TMemSize;
Function GetSys(Index: Integer): TSVCAssemblerItem_Sys;
Function GetConst(Index: Integer): TSVCAssemblerItem_Const;
Function GetVar(Index: Integer): TSVCAssemblerItem_Var;
Function GetLabel(Index: Integer): TSVCAssemblerItem_Label;
Function GetAssemblerLine(Index: Integer): TSVCAssemblerLine;
Function GetAssemblerMessage(Index: Integer): TSVCAssemblerMessage;
Function GetAssemblerMessageTypeCount(MessageType: TSVCParserMessageType): Integer;
protected
Function IndexOfSys(const Identifier: String): Integer; virtual;
Function CheckedIndexOfSys(const Identifier: String): Integer; virtual;
Function IndexOfConst(const Identifier: String): Integer; virtual;
Function IndexOfVar(const Identifier: String): Integer; virtual;
Function IndexOfLabel(const Identifier: String): Integer; virtual;
Function IndexOfUnresolved(const Identifier: String): Integer; virtual;
Function AddDefaultSys(const Identifier: String; Value: TSVCNative): Integer; virtual;
Function AddSys(Item: TSVCAssemblerItem_Sys): Integer; virtual;
Function AddConst(Item: TSVCAssemblerItem_Const): Integer; virtual;
Function AddVar(Item: TSVCAssemblerItem_Var): Integer; virtual;
Function AddLabel(Item: TSVCAssemblerItem_Label): Integer; virtual;
Function AddUnresolved(const Identifier: String; SrcLineIdx, SrcLinePos: Integer): Integer; virtual;
Function RemoveUnresolved(const Identifier: String): Integer; virtual;
Function AddAssemblerLine(Item: TSVCAssemblerLine): Integer; virtual;
procedure AssignSysToConst(const Identifier: String; Value: TSVCComp); virtual;
class Function BuildAssemblerMessage(MessageType: TSVCParserMessageType; Text: String; LineIdx,Position: Integer): TSVCAssemblerMessage; virtual;
Function AddAssemblerMessage(Item: TSVCAssemblerMessage): Integer; virtual;
Function AddWarningMessage(const Text: String; Values: array of const): Integer; overload; virtual;
Function AddWarningMessage(const Text: String): Integer; overload; virtual;
Function AddErrorMessage(const Text: String; Values: array of const; Fatal: Boolean = False): Integer; overload; virtual;
Function AddErrorMessage(const Text: String; Fatal: Boolean = False): Integer; overload; virtual;
procedure LoadDefaultSystemValues; virtual;
procedure LoadPredefinedConstants; virtual;
procedure SortMessages; virtual;
procedure CountMessages; virtual;
procedure FinalizeVariables; virtual;
procedure FinalizeSystemValues; virtual;
procedure FinalizeReplacements; virtual;
procedure ParsingResultHandler(Sender: TObject; ResultType: TSVCParserResultType; Result: Pointer); virtual;
public
constructor Create;
destructor Destroy; override;
procedure Clear(FreeMem: Boolean = False); virtual;
procedure AssembleInit; virtual;
Function AssembleUpdate(const Line: String): Boolean; virtual;
Function AssembleFinal: Boolean; virtual;
Function Assemble(const SourceCode: TStrings; MaxErrorCount: Integer = -1): Boolean; virtual;
Function BuildProgram: TSVCProgram; virtual;
property SystemValues[Index: Integer]: TSVCAssemblerItem_Sys read GetSys;
property Constants[Index: Integer]: TSVCAssemblerItem_Const read GetConst;
property Variables[Index: Integer]: TSVCAssemblerItem_Var read GetVar;
property Labels[Index: Integer]: TSVCAssemblerItem_Label read GetLabel;
property AssemblerLines[Index: Integer]: TSVCAssemblerLine read GetAssemblerLine; default;
property AssemblerMessages[Index: Integer]: TSVCAssemblerMessage read GetAssemblerMessage;
property AssemblerMessageTypeCounts[MessageType: TSVCParserMessageType]: Integer read GetAssemblerMessageTypeCount;
published
property Lists: TSVCListManager read fLists;
property SystemValueCount: Integer read fSystemValues.Count;
property ConstantCount: Integer read fConstants.Count;
property VariableCount: Integer read fVariables.Count;
property LabelCount: Integer read fLabels.Count;
property AssemblerLineCount: Integer read fAssemblerLines.Count;
property AssemblerMessageCount: Integer read fAssemblerMessages.Count;
property CodeSize: TMemSize read fCodeSize;
property MemoryTaken: TMemSize read fMemTaken;
end;
implementation
uses
SysUtils,
SiViC_ASM_Parser_Sys,
SiViC_ASM_Parser_Const,
SiViC_ASM_Parser_Var,
SiViC_ASM_Parser_Data,
SiViC_ASM_Parser_Label;
{$IFDEF FPC_DisableWarns}
{$WARN 4055 OFF} // Conversion between ordinals and pointers is not portable
{$WARN 5024 OFF} // Parameter "$1" not used
{$ENDIF}
type
ESVCAssemblerError = class(Exception);
//------------------------------------------------------------------------------
Function TSVCAssembler.GetSys(Index: Integer): TSVCAssemblerItem_Sys;
begin
If (Index >= Low(fSystemValues.Arr)) and (Index < fSystemValues.Count) then
Result := fSystemValues.Arr[Index]
else
raise Exception.CreateFmt('TSVCAssembler.GetSys: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.GetConst(Index: Integer): TSVCAssemblerItem_Const;
begin
If (Index >= Low(fConstants.Arr)) and (Index < fConstants.Count) then
Result := fConstants.Arr[Index]
else
raise Exception.CreateFmt('TSVCAssembler.GetConst: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.GetVar(Index: Integer): TSVCAssemblerItem_Var;
begin
If (Index >= Low(fVariables.Arr)) and (Index < fVariables.Count) then
Result := fVariables.Arr[Index]
else
raise Exception.CreateFmt('TSVCAssembler.GetVar: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.GetLabel(Index: Integer): TSVCAssemblerItem_Label;
begin
If (Index >= Low(fLabels.Arr)) and (Index < fLabels.Count) then
Result := fLabels.Arr[Index]
else
raise Exception.CreateFmt('TSVCAssembler.GetLabel: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.GetAssemblerLine(Index: Integer): TSVCAssemblerLine;
begin
If (Index >= Low(fAssemblerLines.Arr)) and (Index < fAssemblerLines.Count) then
Result := fAssemblerLines.Arr[Index]
else
raise Exception.CreateFmt('TSVCAssembler.GetAssemblerLine: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.GetAssemblerMessage(Index: Integer): TSVCAssemblerMessage;
begin
If (Index >= Low(fAssemblerMessages.Arr)) and (Index < fAssemblerMessages.Count) then
Result := fAssemblerMessages.Arr[Index]
else
raise Exception.CreateFmt('TSVCAssembler.GetAssemblerMessage: Index (%d) out of bounds.',[Index]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.GetAssemblerMessageTypeCount(MessageType: TSVCParserMessageType): Integer;
begin
If (MessageType >= Low(TSVCParserMessageType)) and (MessageType <= High(TSVCParserMessageType)) then
Result := fAssemblerMessages.Counters[MessageType]
else
raise Exception.CreateFmt('TSVCAssembler.GetAssemblerMessageTypeCount: Unknown message type (%d).',[Ord(MessageType)]);
end;
//==============================================================================
Function TSVCAssembler.IndexOfSys(const Identifier: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := Low(fSystemValues.Arr) to Pred(fSystemValues.Count) do
If AnsiSameText(fSystemValues.Arr[i].Identifier,Identifier) then
begin
Result := i;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.CheckedIndexOfSys(const Identifier: String): Integer;
begin
Result := IndexOfSys(Identifier);
If Result < 0 then
raise Exception.CreateFmt('TSVCAssembler.FinalizeSystemValues: %s value not found.',[Identifier]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.IndexOfConst(const Identifier: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := Low(fConstants.Arr) to Pred(fConstants.Count) do
If AnsiSameText(fConstants.Arr[i].Identifier,Identifier) then
begin
Result := i;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.IndexOfVar(const Identifier: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := Low(fVariables.Arr) to Pred(fVariables.Count) do
If AnsiSameText(fVariables.Arr[i].Identifier,Identifier) then
begin
Result := i;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.IndexOfLabel(const Identifier: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := Low(fLabels.Arr) to Pred(fLabels.Count) do
If AnsiSameText(fLabels.Arr[i].Identifier,Identifier) then
begin
Result := i;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.IndexOfUnresolved(const Identifier: String): Integer;
var
i: Integer;
begin
Result := -1;
For i := Low(fUnresolved.Arr) to Pred(fUnresolved.Count) do
If AnsiSameText(fUnresolved.Arr[i].Identifier,Identifier) then
begin
Result := i;
Break{For i};
end;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddDefaultSys(const Identifier: String; Value: TSVCNative): Integer;
var
SysTemp: TSVCAssemblerItem_Sys;
ConstTemp: TSVCAssemblerItem_Const;
begin
If IndexOfSys(Identifier) < 0 then
begin
SysTemp.Identifier := Identifier;
SysTemp.Value := Value;
SysTemp.Counter := 0;
SysTemp.SrcLineIdx := -1;
Result := AddSys(SysTemp);
// add low word as constant
ConstTemp.Identifier := Identifier + SVC_ASM_ASSEMBLER_LOWORD_SUFFIX;
ConstTemp.Value := TSVCNative(Value);
ConstTemp.Size := vsNative;
ConstTemp.SrcLineIdx := -1;
ConstTemp.SrcLinePos := 0;
AddConst(ConstTemp);
// add high word as constant
ConstTemp.Identifier := Identifier + SVC_ASM_ASSEMBLER_HIWORD_SUFFIX;
ConstTemp.Value := TSVCNative(Value shr 16);
ConstTemp.Size := vsNative;
ConstTemp.SrcLineIdx := -1;
ConstTemp.SrcLinePos := 0;
AddConst(ConstTemp);
end
else raise Exception.Create('TSVCAssembler.AddDefaultSys: Default system value already defined');
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddSys(Item: TSVCAssemblerItem_Sys): Integer;
begin
Result := IndexOfSys(Item.Identifier);
If Result < 0 then
begin
If fSystemValues.Count >= Length(fSystemValues.Arr) then
SetLength(fSystemValues.Arr,Length(fSystemValues.Arr) + 8);
Item.Counter := 0;
fSystemValues.Arr[fSystemValues.Count] := Item;
Result := fSystemValues.Count;
Inc(fSystemValues.Count);
end
else
begin
Inc(fSystemValues.Arr[Result].Counter);
fSystemValues.Arr[Result].Value := Item.Value;
fSystemValues.Arr[Result].SrcLineIdx := Item.SrcLineIdx;
end;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddConst(Item: TSVCAssemblerItem_Const): Integer;
begin
If fConstants.Count >= Length(fConstants.Arr) then
SetLength(fConstants.Arr,Length(fConstants.Arr) + 8);
fConstants.Arr[fConstants.Count] := Item;
Result := fConstants.Count;
Inc(fConstants.Count);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddVar(Item: TSVCAssemblerItem_Var): Integer;
begin
If fVariables.Count >= Length(fVariables.Arr) then
SetLength(fVariables.Arr,Length(fVariables.Arr) + 8);
fVariables.Arr[fVariables.Count] := Item;
Result := fVariables.Count;
Inc(fVariables.Count);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddLabel(Item: TSVCAssemblerItem_Label): Integer;
begin
If fLabels.Count >= Length(fLabels.Arr) then
SetLength(fLabels.Arr,Length(fLabels.Arr) + 8);
fLabels.Arr[fLabels.Count] := Item;
Result := fLabels.Count;
Inc(fLabels.Count);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddUnresolved(const Identifier: String; SrcLineIdx, SrcLinePos: Integer): Integer;
begin
Result := IndexOfUnresolved(Identifier);
If Result < 0 then
begin
If fUnresolved.Count >= Length(fUnresolved.Arr) then
SetLength(fUnresolved.Arr,Length(fUnresolved.Arr) + 8);
fUnresolved.Arr[fUnresolved.Count].Identifier := Identifier;
fUnresolved.Arr[fUnresolved.Count].Count := 1;
fUnresolved.Arr[fUnresolved.Count].SrcLineIdx := SrcLineIdx;
fUnresolved.Arr[fUnresolved.Count].SrcLinePos := SrcLinePos;
Result := fUnresolved.Count;
Inc(fUnresolved.Count);
end
else Inc(fUnresolved.Arr[Result].Count);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.RemoveUnresolved(const Identifier: String): Integer;
var
i: Integer;
begin
Result := IndexOfUnresolved(Identifier);
If Result >= 0 then
begin
Dec(fUnresolved.Arr[Result].Count);
If fUnresolved.Arr[Result].Count <= 0 then
begin
For i := Succ(Result) to Pred(fUnresolved.Count) do
fUnresolved.Arr[i - 1] := fUnresolved.Arr[i];
Dec(fUnresolved.Count);
end;
end;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddAssemblerLine(Item: TSVCAssemblerLine): Integer;
begin
If Length(fAssemblerLines.Arr) <= 0 then
SetLength(fAssemblerLines.Arr,256);
If fAssemblerLines.Count >= Length(fAssemblerLines.Arr) then
SetLength(fAssemblerLines.Arr,Length(fAssemblerLines.Arr) * 2);
fAssemblerLines.Arr[fAssemblerLines.Count] := Item;
Result := fAssemblerLines.Count;
Inc(fAssemblerLines.Count);
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.AssignSysToConst(const Identifier: String; Value: TSVCComp);
var
Index: Integer;
begin
// low word
Index := IndexOfConst(Identifier + SVC_ASM_ASSEMBLER_LOWORD_SUFFIX);
If Index >= 0 then
begin
fConstants.Arr[Index].Value := TSVCNative(Value);
fConstants.Arr[Index].Size := vsNative;
fConstants.Arr[Index].SrcLineIdx := fParsedLineIdx;
end
else AddErrorMessage('System value %s does not have matching lo-word constant',[AnsiUpperCase(Identifier)],True);
// high word
Index := IndexOfConst(Identifier + SVC_ASM_ASSEMBLER_HIWORD_SUFFIX);
If Index >= 0 then
begin
fConstants.Arr[Index].Value := TSVCNative(Value shr 16);
fConstants.Arr[Index].Size := vsNative;
fConstants.Arr[Index].SrcLineIdx := fParsedLineIdx;
end
else AddErrorMessage('System value %s does not have matching hi-word constant',[AnsiUpperCase(Identifier)],True);
end;
//------------------------------------------------------------------------------
class Function TSVCAssembler.BuildAssemblerMessage(MessageType: TSVCParserMessageType; Text: String; LineIdx,Position: Integer): TSVCAssemblerMessage;
begin
Result.MessageType := MessageType;
Result.Text := Text;
Result.LineIdx := LineIdx;
Result.Position := Position;
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddAssemblerMessage(Item: TSVCAssemblerMessage): Integer;
begin
If Length(fAssemblerMessages.Arr) <= 0 then
SetLength(fAssemblerMessages.Arr,128);
If fAssemblerMessages.Count >= Length(fAssemblerMessages.Arr) then
SetLength(fAssemblerMessages.Arr,Length(fAssemblerMessages.Arr) * 2);
fAssemblerMessages.Arr[fAssemblerMessages.Count] := Item;
Result := fAssemblerMessages.Count;
Inc(fAssemblerMessages.Count);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddWarningMessage(const Text: String; Values: array of const): Integer;
var
TempMsg: TSVCAssemblerMessage;
begin
TempMsg.MessageType := pmtWarning;
TempMsg.Text := Format(Text,Values);
TempMsg.LineIdx := fParsedLineIdx;
TempMsg.Position := fParsedLinePos;
Result := AddAssemblerMessage(TempMsg);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddWarningMessage(const Text: String): Integer;
begin
Result := AddWarningMessage(Text,[]);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddErrorMessage(const Text: String; Values: array of const; Fatal: Boolean = False): Integer;
var
TempMsg: TSVCAssemblerMessage;
begin
If not Fatal then
begin
TempMsg.MessageType := pmtError;
TempMsg.Text := Format(Text,Values);
TempMsg.LineIdx := fParsedLineIdx;
TempMsg.Position := fParsedLinePos;
Result := AddAssemblerMessage(TempMsg);
end
else raise ESVCAssemblerError.CreateFmt(Text,Values);
end;
//------------------------------------------------------------------------------
Function TSVCAssembler.AddErrorMessage(const Text: String; Fatal: Boolean = False): Integer;
begin
Result := AddErrorMessage(Text,[],Fatal);
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.LoadDefaultSystemValues;
var
i: Integer;
begin
For i := Low(SVC_PROGRAM_DEFAULTSYSVALS) to High(SVC_PROGRAM_DEFAULTSYSVALS) do
AddDefaultSys(SVC_PROGRAM_DEFAULTSYSVALS[i].Identifier,SVC_PROGRAM_DEFAULTSYSVALS[i].Value);
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.LoadPredefinedConstants;
var
i: Integer;
Temp: TSVCAssemblerItem_Const;
begin
Temp.Size := vsNative;
Temp.SrcLineIdx := -1;
Temp.SrcLinePos := 0;
For i := 0 to Pred(fLists.PredefinedConstantCount) do
begin
Temp.Identifier := fLists.PredefinedConstants[i].Identifier;
Temp.Value := fLists.PredefinedConstants[i].Value;
AddConst(Temp);
end;
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.SortMessages;
procedure Exchange(Idx1,Idx2: Integer);
var
Temp: TSVCAssemblerMessage;
begin
If Idx1 <> Idx2 then
begin
Temp := fAssemblerMessages.Arr[Idx1];
fAssemblerMessages.Arr[Idx1] := fAssemblerMessages.Arr[Idx2];
fAssemblerMessages.Arr[Idx2] := Temp;
end;
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Function CompareItems(Idx1,Idx2: Integer): Integer;
begin
Result := (fAssemblerMessages.Arr[Idx1].LineIdx - fAssemblerMessages.Arr[Idx2].LineIdx) * 10;
If fAssemblerMessages.Arr[Idx1].Position > fAssemblerMessages.Arr[Idx2].Position then
Inc(Result)
else
Dec(Result);
end;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
procedure QuickSort(LeftIdx,RightIdx: Integer);
var
Idx,i: Integer;
begin
If LeftIdx < RightIdx then
begin
Exchange((LeftIdx + RightIdx) shr 1,RightIdx);
Idx := LeftIdx;
For i := LeftIdx to Pred(RightIdx) do
If CompareItems(RightIdx,i) > 0 then
begin
Exchange(i,idx);
Inc(Idx);
end;
Exchange(Idx,RightIdx);
QuickSort(LeftIdx,Idx - 1);
QuickSort(Idx + 1,RightIdx);
end;
end;
begin
If fAssemblerMessages.Count > 1 then
QuickSort(0,Pred(fAssemblerMessages.Count))
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.CountMessages;
var
i: Integer;
begin
For i := Ord(Low(fAssemblerMessages.Counters)) to Ord(High(fAssemblerMessages.Counters)) do
fAssemblerMessages.Counters[TSVCParserMessageType(i)] := 0;
For i := Low(fAssemblerMessages.Arr) to Pred(fAssemblerMessages.Count) do
Inc(fAssemblerMessages.Counters[fAssemblerMessages.Arr[i].MessageType]);
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.FinalizeVariables;
var
CurrAddr: TSVCComp;
i: Integer;
Index: Integer;
Temp: TSVCAssemblerItem_Const;
begin
If fVariables.Count > 0 then
begin
fParsedLineIdx := -1;
fParsedLinePos := 0;
If fMemTaken <= TMemSize(High(TSVCNative) + 1) then
begin
// get address of first variable (lowest address)
CurrAddr := TSVCComp(fMemTaken);
try
// resolve addresses...
For i := Low(fVariables.Arr) to Pred(fVariables.Count) do
begin
fParsedLineIdx := fVariables.Arr[i].SrcLineIdx; // for possible exception
If fVariables.Arr[i].Referencing then
begin
// variable overlap another one
Index := IndexOfVar(fVariables.Arr[i].ReferenceIdentifier);
fParsedLinePos := fVariables.Arr[i].ReferenceIdentifierPos;
If (Index >= 0) and (Index < i) then
begin
fParsedLinePos := 0;
If TSVCComp(fVariables.Arr[Index].Address) + TSVCComp(fVariables.Arr[i].ReferenceOffset) <= TSVCComp(High(TSVCNative)) then
begin
fVariables.Arr[i].Address := TSVCNative(TSVCComp(fVariables.Arr[Index].Address) + TSVCComp(fVariables.Arr[i].ReferenceOffset));
If (TSVCComp(fVariables.Arr[i].Address) + TSVCComp(fVariables.Arr[i].Size)) <= (TSVCComp(High(TSVCNative)) + 1) then
begin
If (TSVCComp(fVariables.Arr[i].Address) + TSVCComp(fVariables.Arr[i].Size)) > CurrAddr then
CurrAddr := TSVCComp(fVariables.Arr[i].Address) + TSVCComp(fVariables.Arr[i].Size);
end
else AddErrorMessage('Variable "%s" cannot fit into memory',[fVariables.Arr[i].Identifier]);
end
else AddErrorMessage('Reference address outside of a valid memory space');
end
else AddErrorMessage('Undeclared identifier "%s"',[fVariables.Arr[i].ReferenceIdentifier]);
end
else
begin
// variable do NOT overlap another one
If (CurrAddr + TSVCComp(fVariables.Arr[i].Size)) <= (TSVCComp(High(TSVCNative)) + 1) then
begin
fVariables.Arr[i].Address := TSVCNative(CurrAddr);
CurrAddr := CurrAddr + TSVCComp(fVariables.Arr[i].Size);
end
else AddErrorMessage('Variable "%s" cannot fit into memory',[fVariables.Arr[i].Identifier]);
end;
// add address of variable as a constant
Temp.Identifier := fVariables.Arr[i].Identifier;
Temp.Value := fVariables.Arr[i].Address;
Temp.Size := vsNative;
Temp.SrcLineIdx := fVariables.Arr[i].SrcLineIdx;
Temp.SrcLinePos := fVariables.Arr[i].SrcLinePos;
AddConst(Temp);
// add size/length of the variable data
Temp.Identifier := fVariables.Arr[i].Identifier + SVC_ASM_ASSEMBLER_LENGTH_SUFFIX;
Temp.Value := fVariables.Arr[i].Size;
Index := IndexOfConst(Temp.Identifier);
If Index < 0 then
AddConst(Temp)
else
AddAssemblerMessage(BuildAssemblerMessage(pmtError,
Format('Identifier "%s" redeclared',[Temp.Identifier]),
fConstants.Arr[Index].SrcLineIdx,fConstants.Arr[Index].SrcLinePos));
end;
finally
fMemTaken := CurrAddr;
end;
end
// line index should be at the end by this point
else AddErrorMessage('No memory available for variables',True);
end;
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.FinalizeSystemValues;
var
Index: Integer;
StackSize: TSVCComp;
MemSize: TSVCComp;
begin
fParsedLinePos := 0;
// check stack size
Index := CheckedIndexOfSys(SVC_PROGRAM_SYSVALNAME_STACKSIZE);
fParsedLineIdx := fSystemValues.Arr[Index].SrcLineIdx;
StackSize := 0;
If fSystemValues.Arr[Index].Value >= TSVCComp(High(TSVCNative) + 1) then
AddErrorMessage('Stack cannot fit into available memory')
else
StackSize := fSystemValues.Arr[Index].Value;
// check and rectify NVmem size
Index := CheckedIndexOfSys(SVC_PROGRAM_SYSVALNAME_NVMEMSIZE);
fParsedLineIdx := fSystemValues.Arr[Index].SrcLineIdx;
If fSystemValues.Arr[Index].Value <= TSVCComp(High(TSVCNative) + 1) then
fSystemValues.Arr[Index].Value := (fSystemValues.Arr[Index].Value + 255) and not TSVCComp($FF)
else
AddErrorMessage('Size of non-volatile memory is out of allowed range');
// check mem size
Index := CheckedIndexOfSys(SVC_PROGRAM_SYSVALNAME_MEMSIZE);
fParsedLineIdx := fSystemValues.Arr[Index].SrcLineIdx;
If fSystemValues.Arr[Index].Value <= TSVCComp(High(TSVCNative) + 1) then
begin
If fSystemValues.Arr[Index].Value <= 0 then
fSystemValues.Arr[Index].Value := 256
else
fSystemValues.Arr[Index].Value := (fSystemValues.Arr[Index].Value + $FF) and not TSVCComp($FF);
end
else AddErrorMessage('Size of allocated memory is out of allowed range');
MemSize := fSystemValues.Arr[Index].Value;
// rectify memory size
fParsedLineIdx := -1;
If (TSVCComp(fMemTaken) + StackSize) > MemSize then
MemSize := (TSVCComp(fMemTaken) + StackSize + $FF) and not TSVCComp($FF);
If MemSize > TSVCComp(High(TSVCNative) + 1) then
AddErrorMessage('Program cannot fit into available memory');
fSystemValues.Arr[Index].Value := MemSize;
AssignSysToConst(SVC_PROGRAM_SYSVALNAME_MEMSIZE,MemSize);
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.FinalizeReplacements;
var
i,j: Integer;
Index: Integer;
Value: TSVCComp;
begin
For i := Low(fAssemblerLines.Arr) to Pred(fAssemblerLines.Count) do
If fAssemblerLines.Arr[i].LineType in [altInstr,altData] then
For j := Low(fAssemblerLines.Arr[i].Instruction.Replacements) to
High(fAssemblerLines.Arr[i].Instruction.Replacements) do
with fAssemblerLines.Arr[i].Instruction.Replacements[j] do
begin
// prepare for possible exception
fParsedLine := fAssemblerLines.Arr[i].Str;
fParsedLineIdx := fAssemblerLines.Arr[i].SrcLineIdx;
fParsedLinePos := LinePos;
Value := 0;
If IsLabel then
begin
// replacement is a label, true value needs to be calculated
Index := IndexOfLabel(Identifier);
If Index >= 0 then
begin
Value := TSVCComp(fLabels.Arr[Index].Address) -
(TSVCComp(fAssemblerLines.Arr[i].Instruction.Address) +
Length(fAssemblerLines.Arr[i].Instruction.Data));
case ValType of
iatREL8: If not fParser.CheckRange(Value,vsByte) then
AddWarningMessage('Memory offset out of allowed range for label "%s"',[Identifier]);
iatREL16: If not fParser.CheckRange(Value,vsWord) then
AddWarningMessage('Memory offset out of allowed range for label "%s"',[Identifier]);
else
AddErrorMessage('Invalid value type for label "%s"',[Identifier]);
end;
end
else AddErrorMessage('Undeclared label "%s"',[Identifier]);
end
else
begin
// replacement is a constant or address of variable
Index := IndexOfConst(Identifier);
If Index >= 0 then
begin
If IndexOfVar(Identifier) >= 0 then
begin
// replacement is an address of variable (value is taken from constant of the same name)
Value := TSVCComp(TSVCSNative(fConstants.Arr[Index].Value));
case ValType of
iatREL8: begin
AddAssemblerMessage(BuildAssemblerMessage(pmtHint,
'Assigning address of a variable into relative offset',fParsedLineIdx,fParsedLinePos));
If not fParser.CheckRange(Value,vsByte) then
AddWarningMessage('Address of variable "%s" out of alloved range for this argument',[Identifier]);
end;
iatIMM8: If not fParser.CheckRange(Value,vsByte) then
AddWarningMessage('Address of variable "%s" out of alloved range for this argument',[Identifier]);
iatREL16: begin
AddAssemblerMessage(BuildAssemblerMessage(pmtHint,
'Assigning address of a variable into relative offset',fParsedLineIdx,fParsedLinePos));
If not fParser.CheckRange(Value,vsWord) then
AddWarningMessage('Address of variable "%s" out of alloved range for this argument',[Identifier]);
end;
iatIMM16: If not fParser.CheckRange(Value,vsWord) then
AddWarningMessage('Address of variable "%s" out of alloved range for this argument',[Identifier]);
else
AddErrorMessage('Invalid value type for replacement "%s"',[Identifier]);
end;
end
else
begin
// replacement is a constant
Value := TSVCComp(TSVCSNative(fConstants.Arr[Index].Value));
case ValType of
iatREL8,
iatIMM8: If not fParser.CheckRange(Value,vsByte) then
AddWarningMessage('Value of constant "%s" out of alloved range for this argument',[Identifier]);
iatREL16,
iatIMM16: If not fParser.CheckRange(Value,vsWord) then
AddWarningMessage('Value of constant "%s" out of alloved range for this argument',[Identifier]);
else
AddErrorMessage('Invalid value type for replacement "%s"',[Identifier]);
end;
end;
end
else AddErrorMessage('Undeclared identifier "%s"',[Identifier]);
end; {If IsLabel then - else - end}
// store value of replacement into the code
If ValType in [iatREL8,iatIMM8] then
begin
If Position <= High(fAssemblerLines.Arr[i].Instruction.Data) then
fAssemblerLines.Arr[i].Instruction.Data[Position] := TSVCByte(Value)
else
AddErrorMessage('Replacement for identifier "%s" cannot fit into code stream',[Identifier]);
end
else If ValType in [iatREL16,iatIMM16] then
begin
If Position < High(fAssemblerLines.Arr[i].Instruction.Data) then
begin
fAssemblerLines.Arr[i].Instruction.Data[Position] := TSVCBYte(Value);
fAssemblerLines.Arr[i].Instruction.Data[Position + 1] := TSVCBYte(Value shr 8);
end
else AddErrorMessage('Replacement for identifier "%s" cannot fit into code stream',[Identifier]);
end
else AddErrorMessage('Invalid argument type for replacement "%s"',[Identifier]);
// remove replacement from unresolved
RemoveUnresolved(Identifier);
end;
end;
//------------------------------------------------------------------------------
procedure TSVCAssembler.ParsingResultHandler(Sender: TObject; ResultType: TSVCParserResultType; Result: Pointer);
var
i: Integer;
TempConst: TSVCAssemblerItem_Const;
TempVar: TSVCAssemblerItem_Var;
TempLabel: TSVCAssemblerItem_Label;
TempLine: TSVCAssemblerLine;
begin
fParsedLinePos := 0;
// initialize temporary storage for assembler line
TempLine.Str := fParsedLine;
TempLine.SrcLineIdx := fParsedLineIdx;
TempLine.LineType := altOther;
TempLine.Instruction.Address := 0;
SetLength(TempLine.Instruction.Data,0);
SetLength(TempLine.Instruction.Replacements,0);
case ResultType of
prtNone:; // no valid result, do nothing
prtSys: begin // system value
i := IndexOfSys(PSVCParserResult_Sys(Result)^.Identifier);
fParsedLinePos := PSVCParserResult_Sys(Result)^.IdentifierPos;
If i >= 0 then
begin
If fSystemValues.Arr[i].Counter > 0 then
AddWarningMessage('System value %s already assigned',[AnsiUpperCase(fSystemValues.Arr[i].Identifier)]);
fSystemValues.Arr[i].Value := PSVCParserResult_Sys(Result)^.Value;
Inc(fSystemValues.Arr[i].Counter);
fSystemValues.Arr[i].SrcLineIdx := fParsedLineIdx;
// assign value to appropriate constant
AssignSysToConst(fSystemValues.Arr[i].Identifier,fSystemValues.Arr[i].Value);
end
else AddErrorMessage('Unknown system value %s',[AnsiUpperCase(PSVCParserResult_Sys(Result)^.Identifier)],True);
TempLine.LineType := altSys;
AddAssemblerLine(TempLine);
end;
prtConst: begin // constant
fParsedLinePos := PSVCParserResult_Const(Result)^.IdentifierPos;
If (IndexOfConst(PSVCParserResult_Const(Result)^.Identifier) < 0) and (IndexOfVar(PSVCParserResult_Const(Result)^.Identifier) < 0) then
begin
TempConst.Identifier := PSVCParserResult_Const(Result)^.Identifier;
TempConst.Value := PSVCParserResult_Const(Result)^.Value;
TempConst.Size := PSVCParserResult_Const(Result)^.Size;
TempConst.SrcLineIdx := fParsedLineIdx;
TempConst.SrcLinePos := fParsedLinePos;
AddConst(TempConst);
TempLine.LineType := altConst;
AddAssemblerLine(TempLine);
end
else AddErrorMessage('Identifier "%s" redeclared',[PSVCParserResult_Const(Result)^.Identifier]);
end;
prtVar: begin // variable
fParsedLinePos := PSVCParserResult_Var(Result)^.IdentifierPos;
If (IndexOfConst(PSVCParserResult_Var(Result)^.Identifier) < 0) and (IndexOfVar(PSVCParserResult_Var(Result)^.Identifier) < 0) then
begin
TempVar.Identifier := PSVCParserResult_Var(Result)^.Identifier;
TempVar.Size := PSVCParserResult_Var(Result)^.Size;
TempVar.Data := PSVCParserResult_Var(Result)^.Data;
TempVar.Referencing := PSVCParserResult_Var(Result)^.Referencing;