Skip to content

Commit 2ed5a43

Browse files
committed
Added code to track source code line numbers at the bytecode level
1 parent dac4f6d commit 2ed5a43

File tree

6 files changed

+181
-172
lines changed

6 files changed

+181
-172
lines changed

Rhodus_Version_3/uAST.pas

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ interface
1818
type
1919
TASTNode = class;
2020
TChildNodes = class (TList<TASTNode>) // TList allows me to handle freeing of objects myself
21+
lineNumber : integer;
2122
procedure freeChildNodes;
2223
end;
2324

Rhodus_Version_3/uBuiltInStr.pas

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ constructor TBuiltInStr.Create;
3232
begin
3333
inherited Create ('strings', 'String Module');
3434

35-
3635
addStringValue('ascii_lower', 'abcdefghijklmnopqrstuv', 'lower ascii characters', true);
3736
addStringValue('ascii_upper', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'upper ascii characters', true);
3837
addStringValue('digits', '0123456789', 'digits', true);

Rhodus_Version_3/uCompile.pas

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ procedure TCompiler.compileForStatement(node: TASTFor);
251251
if node.iterationBlock.direction.nodeType = ntTo then
252252
begin
253253
if compilingFunction then
254-
code.addByteCode(oLocalInc, localSymbolIndex)
254+
code.addByteCode(oLocalInc, localSymbolIndex, node.iterationBlock.direction.lineNumber)
255255
else
256256
code.addByteCode(oInc, symbol.symbolName, node.iterationBlock.stepValue)
257257
end
@@ -372,11 +372,9 @@ procedure TCompiler.compileAssignment(node: TASTAssignment);
372372
inAssignment := True;
373373
try
374374
compileCode (node.leftSide);
375-
// emit the store method here?
376375
finally
377376
inAssignment := False
378377
end;
379-
380378
end
381379
else
382380
raise ECompilerException.Create('Internal Error in compileAssignment', 0, 0)
@@ -486,18 +484,18 @@ procedure TCompiler.compileSubscripts(subscripts: TChildNodes);
486484
begin
487485
// Special case, we only save if its the last index, otherwise we load
488486
if i = subscripts.Count - 1 then
489-
code.addByteCode(oSvecIdx, subscripts.Count)
487+
code.addByteCode(oSvecIdx, subscripts.Count, subscripts[i].lineNumber)
490488
else
491-
code.addByteCode(oLvecIdx, subscripts.Count);
489+
code.addByteCode(oLvecIdx, subscripts.Count, subscripts.lineNumber);
492490
end
493491
else
494492
begin
495493
if not slicing then
496-
code.addByteCode(oLvecIdx, subscripts.Count)
494+
code.addByteCode(oLvecIdx, subscripts.Count, subscripts.lineNumber)
497495
end;
498496
end;
499497
if slicing then
500-
code.addByteCode(oSliceObj, subscripts.Count);
498+
code.addByteCode(oSliceObj, subscripts.Count, subscripts.lineNumber);
501499

502500
if slicing then
503501
slicingSubscripts.Pop;
@@ -556,10 +554,10 @@ procedure TCompiler.compileList(node: TASTCreatelist);
556554
begin
557555
for i := 0 to node.list.Count - 1 do
558556
compileCode(node.list[i]);
559-
code.addByteCode(oCreateList, node.list.Count);
557+
code.addByteCode(oCreateList, node.list.Count, node.lineNumber);
560558
end
561559
else
562-
code.addByteCode(oCreateList, 0); // empty list
560+
code.addByteCode(oCreateList, 0, node.lineNumber); // empty list
563561
end;
564562

565563

@@ -634,15 +632,15 @@ procedure TCompiler.compilePrintStatement(node: TASTNode);
634632
for i := 0 to (node as TASTPrint).argumentList.list.Count - 1 do
635633
compileCode((node as TASTPrint).argumentList.list[i]);
636634

637-
code.addByteCode(oPushi, (node as TASTPrint).argumentList.list.Count);
635+
code.addByteCode(oPushi, (node as TASTPrint).argumentList.list.Count, node.lineNumber);
638636
code.addByteCode(oPrint);
639637
end
640638
else
641639
begin
642640
for i := 0 to (node as TASTPrintLn).argumentList.list.Count - 1 do
643641
compileCode((node as TASTPrintLn).argumentList.list[i]);
644642

645-
code.addByteCode(oPushi, (node as TASTPrintLn).argumentList.list.Count);
643+
code.addByteCode(oPushi, (node as TASTPrintLn).argumentList.list.Count, node.lineNumber);
646644
code.addByteCode(oPrintln);
647645
end;
648646
end;
@@ -701,7 +699,7 @@ procedure TCompiler.compileSwitchStatement(node: TASTSwitch);
701699
for i := 0 to listOfCaseStatements.list.Count - 1 do
702700
begin
703701
code.addByteCode(oDup);
704-
code.addByteCode(oPushi, caseValues[i]);
702+
code.addByteCode(oPushi, caseValues[i], node.lineNumber);
705703
code.addByteCode(oIsEq);
706704
jumpToLocation[i] := code.addByteCode(oJmpIfTrue);
707705
end;
@@ -877,7 +875,7 @@ procedure TCompiler.compileRightHandSide (node : TASTIdentifier);
877875
// Check if its in the local space, if yes then emit the local load opecode
878876
// if not it could be in the module space
879877
if currentUserFunction.localSymbolTable.find(node.symbolName, localSymbolIndex) then
880-
code.addByteCode(oLoadLocal, localSymbolIndex)
878+
code.addByteCode(oLoadLocal, localSymbolIndex, node.lineNumber)
881879
else
882880
begin
883881
if currentModule.symbolTable.find(node.symbolName, symbol) then
@@ -913,9 +911,9 @@ procedure TCompiler.compileLeftHandSide (node : TASTIdentifier);
913911
if not currentUserFunction.localSymbolTable.find(node.symbolName, localSymbolIndex) then
914912
localSymbolIndex := currentUserFunction.localSymbolTable.addSymbol (node.symbolName);
915913
if inAssignment_NextToEquals then
916-
code.addByteCode (oStoreLocal, localSymbolIndex)
914+
code.addByteCode (oStoreLocal, localSymbolIndex, node.lineNumber)
917915
else
918-
code.addByteCode (oLoadLocal, localSymbolIndex)
916+
code.addByteCode (oLoadLocal, localSymbolIndex, node.lineNumber)
919917
end
920918
end
921919
else
@@ -980,7 +978,7 @@ procedure TCompiler.compilePrimaryFunction (node : TASTPrimaryFunction);
980978
for anode in node.argumentList.list do
981979
compileCode (anode);
982980

983-
code.addByteCode(oCall, node.argumentList.list.Count);
981+
code.addByteCode(oCall, node.argumentList.list.Count, node.lineNumber);
984982
compileCode (node.primaryPlus);
985983
end;
986984

@@ -1041,7 +1039,7 @@ procedure TCompiler.compileCode(node: TASTNode);
10411039
ntSliceAll:
10421040
compileSliceAll (node);
10431041
ntSliceEqual:
1044-
code.addByteCode(oPushi, SLICE_EQUAL);
1042+
code.addByteCode(oPushi, SLICE_EQUAL, 0);
10451043
ntCreateList:
10461044
compileList(node as TASTCreatelist);
10471045
ntAssignment:
@@ -1123,16 +1121,16 @@ procedure TCompiler.compileCode(node: TASTNode);
11231121
ntBoolean:
11241122
code.addByteCode(oPushb, (node as TASTBoolean).bValue);
11251123
ntInteger:
1126-
code.addByteCode(oPushi, (node as TASTInteger).iValue);
1124+
code.addByteCode(oPushi, (node as TASTInteger).iValue, node.lineNumber);
11271125
ntFloat:
11281126
begin
11291127
index := currentModule.moduleProgram.constantValueTable.Add (TConstantValueElement.Create((node as TASTFloat).dValue));
1130-
code.addByteCode(oPushd, index);
1128+
code.addByteCode(oPushd, index, node.lineNumber);
11311129
end;
11321130
ntString :
11331131
begin
11341132
index := currentmodule.moduleProgram.constantValueTable.Add (TConstantValueElement.Create ((node as TASTString).sValue));
1135-
code.addByteCode(oPushs, index);
1133+
code.addByteCode(oPushs, index, node.lineNumber);
11361134
end;
11371135
ntBreak:
11381136
// place holder for the jmp instruction

Rhodus_Version_3/uProgramCode.pas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ interface
1919
float : double; // This is currently only used by the inc and dec op codes
2020
moduleName : string;// Use by import module
2121
symbolName : string;
22+
lineNumber : integer;
2223
end;
2324
TCode = TArray<TByteCode>;
2425

@@ -36,7 +37,7 @@ TProgram = class (TObject)
3637
procedure append (byteCode : TByteCode);
3738
procedure compactCode;
3839
function addByteCode (opCode : TOpCode) : integer; overload;
39-
procedure addByteCode (opCode : TOpCode; iValue : integer); overload;
40+
procedure addByteCode (opCode : TOpCode; iValue, lineNumber : integer); overload;
4041
procedure addByteCode (opCode : TOpCode; bValue : boolean); overload;
4142
procedure addByteCode (opCode : TOpCode; const symbolName : string; increment : double); overload;
4243
procedure addModuleByteCode (opCode : TOpCode; const moduleName : string);
@@ -187,10 +188,11 @@ function TProgram.addByteCode (opCode : TOpCode) : integer;
187188
end;
188189

189190

190-
procedure TProgram.addByteCode (opCode : TOpCode; iValue : integer);
191+
procedure TProgram.addByteCode (opCode : TOpCode; iValue, lineNumber : integer);
191192
begin
192193
checkSpace;
193194
code[actualLength] := createByteCode (opCode, iValue);
195+
code[actualLength].lineNumber := lineNumber;
194196
inc(actualLength);
195197
end;
196198

VirtualMachine/uAssembler.pas

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,19 @@ function assembleCode (const srcCode : string) : TProgram;
158158
// continue;
159159
// end;
160160
if opCodeStr = opCodeNames[oStoreSymbol] then begin
161-
result.addByteCode (oStoreSymbol, strtoint (opCodeArgument));
161+
result.addByteCode (oStoreSymbol, strtoint (opCodeArgument), 0);
162162
inc (instCounter);
163163
continue;
164164
end;
165165
if opCodeStr = opCodeNames[oCreateList] then
166166
begin
167-
result.addByteCode (oCreateList, strtoint (opCodeArgument));
167+
result.addByteCode (oCreateList, strtoint (opCodeArgument), 0);
168168
inc (instCounter);
169169
continue
170170
end;
171171
if opCodeStr = opCodeNames[oPushi] then
172172
begin
173-
result.addByteCode (oPushi, strtoint (opCodeArgument));
173+
result.addByteCode (oPushi, strtoint (opCodeArgument), 0);
174174
inc (instCounter);
175175
continue;
176176
end;
@@ -215,7 +215,7 @@ function assembleCode (const srcCode : string) : TProgram;
215215
if index <> -1 then
216216
begin
217217
labelInt := integer (labels.Objects[index]);
218-
result.addByteCode (oJMP, labelInt - instCounter);
218+
result.addByteCode (oJMP, labelInt - instCounter, 0);
219219
end
220220
else
221221
raise Exception.Create ('Unable to locate label specificed in jmp opcode');
@@ -228,7 +228,7 @@ function assembleCode (const srcCode : string) : TProgram;
228228
if index <> -1 then
229229
begin
230230
labelInt := integer (labels.Objects[index]);
231-
result.addByteCode (oJmpIfTrue, labelInt - instCounter);
231+
result.addByteCode (oJmpIfTrue, labelInt - instCounter, 0);
232232
end
233233
else
234234
raise Exception.Create ('Unable to locate label specificed in jmpIfTrue opcode');
@@ -242,7 +242,7 @@ function assembleCode (const srcCode : string) : TProgram;
242242
if index <> -1 then
243243
begin
244244
labelInt := integer (labels.Objects[index]);
245-
result.addByteCode (oJmpIfFalse, labelInt - instCounter);
245+
result.addByteCode (oJmpIfFalse, labelInt - instCounter, 0);
246246
end
247247
else
248248
raise Exception.Create ('Unable to locate label specificed in jmpIfFalse opcode');

0 commit comments

Comments
 (0)