From 18d0d1a2af82e0404913a0d174f28829d0c3be4c Mon Sep 17 00:00:00 2001 From: Neta London <67196883+netalondon@users.noreply.github.com> Date: Tue, 17 Sep 2024 17:24:15 +0300 Subject: [PATCH] Add class name as prefix to VM labels in the compiler (#465) --- projects/src/samples/project_11/average.ts | 8 +- .../src/samples/project_11/complex_arrays.ts | 16 +-- .../src/samples/project_11/convert_to_bin.ts | 40 +++--- projects/src/samples/project_11/pong/ball.ts | 136 +++++++++--------- projects/src/samples/project_11/pong/bat.ts | 24 ++-- .../src/samples/project_11/pong/pong_game.ts | 96 ++++++------- .../src/samples/project_11/square/square.ts | 48 +++---- .../samples/project_11/square/square_game.ts | 112 +++++++-------- simulator/src/jack/compiler.test.ts | 9 +- simulator/src/jack/compiler.ts | 2 +- 10 files changed, 246 insertions(+), 245 deletions(-) diff --git a/projects/src/samples/project_11/average.ts b/projects/src/samples/project_11/average.ts index 4319611bf..6279b69a5 100644 --- a/projects/src/samples/project_11/average.ts +++ b/projects/src/samples/project_11/average.ts @@ -334,12 +334,12 @@ export const compiled = `function Main.main 4 pop local 0 push constant 0 pop local 2 -label L0 +label Main_0 push local 2 push local 1 lt not - if-goto L1 + if-goto Main_1 push local 2 push local 0 add @@ -394,8 +394,8 @@ label L0 push constant 1 add pop local 2 - goto L0 -label L1 + goto Main_0 +label Main_1 push constant 15 call String.new 1 push constant 84 diff --git a/projects/src/samples/project_11/complex_arrays.ts b/projects/src/samples/project_11/complex_arrays.ts index 1a86931ef..942e2bd01 100644 --- a/projects/src/samples/project_11/complex_arrays.ts +++ b/projects/src/samples/project_11/complex_arrays.ts @@ -1472,7 +1472,7 @@ export const compiled = `function Main.main 3 push constant 0 eq not - if-goto L1 + if-goto Main_1 push local 0 push constant 10 call Main.fill 2 @@ -1529,9 +1529,9 @@ export const compiled = `function Main.main 3 pop pointer 1 push temp 0 pop that 0 - goto L0 -label L1 -label L0 + goto Main_0 +label Main_1 +label Main_0 push constant 44 call String.new 1 push constant 84 @@ -1744,12 +1744,12 @@ function Main.double 0 call Math.multiply 2 return function Main.fill 0 -label L2 +label Main_2 push argument 1 push constant 0 gt not - if-goto L3 + if-goto Main_3 push argument 1 push constant 1 sub @@ -1763,7 +1763,7 @@ label L2 pop pointer 1 push temp 0 pop that 0 - goto L2 -label L3 + goto Main_2 +label Main_3 push constant 0 return`; diff --git a/projects/src/samples/project_11/convert_to_bin.ts b/projects/src/samples/project_11/convert_to_bin.ts index 821a21f09..be1296c9a 100644 --- a/projects/src/samples/project_11/convert_to_bin.ts +++ b/projects/src/samples/project_11/convert_to_bin.ts @@ -645,10 +645,10 @@ function Main.convert 3 push constant 1 neg pop local 2 -label L0 +label Main_0 push local 2 not - if-goto L1 + if-goto Main_1 push local 1 push constant 1 add @@ -661,7 +661,7 @@ label L0 gt not not - if-goto L3 + if-goto Main_3 push argument 0 push local 0 and @@ -669,29 +669,29 @@ label L0 eq not not - if-goto L5 + if-goto Main_5 push constant 8000 push local 1 add push constant 1 call Memory.poke 2 pop temp 0 - goto L4 -label L5 + goto Main_4 +label Main_5 push constant 8000 push local 1 add push constant 0 call Memory.poke 2 pop temp 0 -label L4 - goto L2 -label L3 +label Main_4 + goto Main_2 +label Main_3 push constant 0 pop local 2 -label L2 - goto L0 -label L1 +label Main_2 + goto Main_0 +label Main_1 push constant 0 return function Main.nextMask 0 @@ -699,23 +699,23 @@ function Main.nextMask 0 push constant 0 eq not - if-goto L7 + if-goto Main_7 push constant 1 return - goto L6 -label L7 + goto Main_6 +label Main_7 push argument 0 push constant 2 call Math.multiply 2 return -label L6 +label Main_6 function Main.fillMemory 0 -label L8 +label Main_8 push argument 1 push constant 0 gt not - if-goto L9 + if-goto Main_9 push argument 0 push argument 2 call Memory.poke 2 @@ -728,7 +728,7 @@ label L8 push constant 1 add pop argument 0 - goto L8 -label L9 + goto Main_8 +label Main_9 push constant 0 return`; diff --git a/projects/src/samples/project_11/pong/ball.ts b/projects/src/samples/project_11/pong/ball.ts index 244d88c69..789f1c9ea 100644 --- a/projects/src/samples/project_11/pong/ball.ts +++ b/projects/src/samples/project_11/pong/ball.ts @@ -2631,7 +2631,7 @@ function Ball.setDestination 3 pop this 7 push this 7 not - if-goto L1 + if-goto Ball_1 push local 0 pop local 2 push local 1 @@ -2646,8 +2646,8 @@ function Ball.setDestination 3 push argument 1 lt pop this 9 - goto L0 -label L1 + goto Ball_0 +label Ball_1 push this 0 push argument 1 lt @@ -2656,7 +2656,7 @@ label L1 push argument 2 lt pop this 9 -label L0 +label Ball_0 push constant 2 push local 1 call Math.multiply 2 @@ -2685,138 +2685,138 @@ function Ball.move 0 push constant 0 lt not - if-goto L3 + if-goto Ball_3 push this 4 push this 5 add pop this 4 - goto L2 -label L3 + goto Ball_2 +label Ball_3 push this 4 push this 6 add pop this 4 push this 9 not - if-goto L5 + if-goto Ball_5 push this 7 not - if-goto L7 + if-goto Ball_7 push this 0 push constant 4 add pop this 0 - goto L6 -label L7 + goto Ball_6 +label Ball_7 push this 1 push constant 4 add pop this 1 -label L6 - goto L4 -label L5 +label Ball_6 + goto Ball_4 +label Ball_5 push this 7 not - if-goto L9 + if-goto Ball_9 push this 0 push constant 4 sub pop this 0 - goto L8 -label L9 + goto Ball_8 +label Ball_9 push this 1 push constant 4 sub pop this 1 -label L8 -label L4 -label L2 +label Ball_8 +label Ball_4 +label Ball_2 push this 8 not - if-goto L11 + if-goto Ball_11 push this 7 not - if-goto L13 + if-goto Ball_13 push this 1 push constant 4 add pop this 1 - goto L12 -label L13 + goto Ball_12 +label Ball_13 push this 0 push constant 4 add pop this 0 -label L12 - goto L10 -label L11 +label Ball_12 + goto Ball_10 +label Ball_11 push this 7 not - if-goto L15 + if-goto Ball_15 push this 1 push constant 4 sub pop this 1 - goto L14 -label L15 + goto Ball_14 +label Ball_15 push this 0 push constant 4 sub pop this 0 -label L14 -label L10 +label Ball_14 +label Ball_10 push this 0 push this 10 gt not not - if-goto L17 + if-goto Ball_17 push constant 1 pop this 14 push this 10 pop this 0 - goto L16 -label L17 -label L16 + goto Ball_16 +label Ball_17 +label Ball_16 push this 0 push this 11 lt not not - if-goto L19 + if-goto Ball_19 push constant 2 pop this 14 push this 11 pop this 0 - goto L18 -label L19 -label L18 + goto Ball_18 +label Ball_19 +label Ball_18 push this 1 push this 12 gt not not - if-goto L21 + if-goto Ball_21 push constant 3 pop this 14 push this 12 pop this 1 - goto L20 -label L21 -label L20 + goto Ball_20 +label Ball_21 +label Ball_20 push this 1 push this 13 lt not not - if-goto L23 + if-goto Ball_23 push constant 4 pop this 14 push this 13 pop this 1 - goto L22 -label L23 -label L22 + goto Ball_22 +label Ball_23 +label Ball_22 push pointer 0 call Ball.show 1 pop temp 0 @@ -2837,11 +2837,11 @@ function Ball.bounce 5 push constant 0 eq not - if-goto L25 + if-goto Ball_25 push constant 10 pop local 4 - goto L24 -label L25 + goto Ball_24 +label Ball_25 push this 2 push constant 0 lt @@ -2860,20 +2860,20 @@ label L25 and or not - if-goto L27 + if-goto Ball_27 push constant 20 pop local 4 - goto L26 -label L27 + goto Ball_26 +label Ball_27 push constant 5 pop local 4 -label L26 -label L24 +label Ball_26 +label Ball_24 push this 14 push constant 1 eq not - if-goto L29 + if-goto Ball_29 push constant 506 pop local 0 push local 3 @@ -2889,13 +2889,13 @@ label L24 call Math.multiply 2 add pop local 1 - goto L28 -label L29 + goto Ball_28 +label Ball_29 push this 14 push constant 2 eq not - if-goto L31 + if-goto Ball_31 push constant 0 pop local 0 push local 3 @@ -2910,13 +2910,13 @@ label L29 call Math.multiply 2 add pop local 1 - goto L30 -label L31 + goto Ball_30 +label Ball_31 push this 14 push constant 3 eq not - if-goto L33 + if-goto Ball_33 push constant 250 pop local 1 push local 2 @@ -2932,8 +2932,8 @@ label L31 call Math.multiply 2 add pop local 0 - goto L32 -label L33 + goto Ball_32 +label Ball_33 push constant 0 pop local 1 push local 2 @@ -2948,9 +2948,9 @@ label L33 call Math.multiply 2 add pop local 0 -label L32 -label L30 -label L28 +label Ball_32 +label Ball_30 +label Ball_28 push pointer 0 push local 0 push local 1 diff --git a/projects/src/samples/project_11/pong/bat.ts b/projects/src/samples/project_11/pong/bat.ts index d218f1488..fb8188a44 100644 --- a/projects/src/samples/project_11/pong/bat.ts +++ b/projects/src/samples/project_11/pong/bat.ts @@ -1267,7 +1267,7 @@ function Bat.move 0 push constant 1 eq not - if-goto L1 + if-goto Bat_1 push this 0 push constant 4 sub @@ -1276,12 +1276,12 @@ function Bat.move 0 push constant 0 lt not - if-goto L3 + if-goto Bat_3 push constant 0 pop this 0 - goto L2 -label L3 -label L2 + goto Bat_2 +label Bat_3 +label Bat_2 push constant 0 call Screen.setColor 1 pop temp 0 @@ -1315,8 +1315,8 @@ label L2 add call Screen.drawRectangle 4 pop temp 0 - goto L0 -label L1 + goto Bat_0 +label Bat_1 push this 0 push constant 4 add @@ -1327,14 +1327,14 @@ label L1 push constant 511 gt not - if-goto L5 + if-goto Bat_5 push constant 511 push this 2 sub pop this 0 - goto L4 -label L5 -label L4 + goto Bat_4 +label Bat_5 +label Bat_4 push constant 0 call Screen.setColor 1 pop temp 0 @@ -1368,6 +1368,6 @@ label L4 add call Screen.drawRectangle 4 pop temp 0 -label L0 +label Bat_0 push constant 0 return`; diff --git a/projects/src/samples/project_11/pong/pong_game.ts b/projects/src/samples/project_11/pong/pong_game.ts index 95acb2824..ad45e37dc 100644 --- a/projects/src/samples/project_11/pong/pong_game.ts +++ b/projects/src/samples/project_11/pong/pong_game.ts @@ -1689,12 +1689,12 @@ function PongGame.getInstance 0 function PongGame.run 1 push argument 0 pop pointer 0 -label L0 +label PongGame_0 push this 3 not not - if-goto L1 -label L2 + if-goto PongGame_1 +label PongGame_2 push local 0 push constant 0 eq @@ -1702,7 +1702,7 @@ label L2 not and not - if-goto L3 + if-goto PongGame_3 call Keyboard.keyPressed 0 pop local 0 push this 0 @@ -1714,44 +1714,44 @@ label L2 push constant 50 call Sys.wait 1 pop temp 0 - goto L2 -label L3 + goto PongGame_2 +label PongGame_3 push local 0 push constant 130 eq not - if-goto L5 + if-goto PongGame_5 push this 0 push constant 1 call Bat.setDirection 2 pop temp 0 - goto L4 -label L5 + goto PongGame_4 +label PongGame_5 push local 0 push constant 132 eq not - if-goto L7 + if-goto PongGame_7 push this 0 push constant 2 call Bat.setDirection 2 pop temp 0 - goto L6 -label L7 + goto PongGame_6 +label PongGame_7 push local 0 push constant 140 eq not - if-goto L9 + if-goto PongGame_9 push constant 1 neg pop this 3 - goto L8 -label L9 -label L8 -label L6 -label L4 -label L10 + goto PongGame_8 +label PongGame_9 +label PongGame_8 +label PongGame_6 +label PongGame_4 +label PongGame_10 push local 0 push constant 0 eq @@ -1760,7 +1760,7 @@ label L10 not and not - if-goto L11 + if-goto PongGame_11 call Keyboard.keyPressed 0 pop local 0 push this 0 @@ -1772,13 +1772,13 @@ label L10 push constant 50 call Sys.wait 1 pop temp 0 - goto L10 -label L11 - goto L0 -label L1 + goto PongGame_10 +label PongGame_11 + goto PongGame_0 +label PongGame_1 push this 3 not - if-goto L13 + if-goto PongGame_13 push constant 10 push constant 27 call Output.moveCursor 2 @@ -1805,9 +1805,9 @@ label L1 call String.appendChar 2 call Output.printString 1 pop temp 0 - goto L12 -label L13 -label L12 + goto PongGame_12 +label PongGame_13 +label PongGame_12 push constant 0 return function PongGame.moveBall 5 @@ -1825,7 +1825,7 @@ function PongGame.moveBall 5 not and not - if-goto L15 + if-goto PongGame_15 push this 2 pop this 5 push constant 0 @@ -1846,7 +1846,7 @@ function PongGame.moveBall 5 push constant 4 eq not - if-goto L17 + if-goto PongGame_17 push local 1 push local 4 gt @@ -1858,32 +1858,32 @@ function PongGame.moveBall 5 push this 3 not not - if-goto L19 + if-goto PongGame_19 push local 4 push local 1 push constant 10 add lt not - if-goto L21 + if-goto PongGame_21 push constant 1 neg pop local 0 - goto L20 -label L21 + goto PongGame_20 +label PongGame_21 push local 3 push local 2 push constant 10 sub gt not - if-goto L23 + if-goto PongGame_23 push constant 1 pop local 0 - goto L22 -label L23 -label L22 -label L20 + goto PongGame_22 +label PongGame_23 +label PongGame_22 +label PongGame_20 push this 6 push constant 2 sub @@ -1903,18 +1903,18 @@ label L20 push this 4 call Output.printInt 1 pop temp 0 - goto L18 -label L19 -label L18 - goto L16 -label L17 -label L16 + goto PongGame_18 +label PongGame_19 +label PongGame_18 + goto PongGame_16 +label PongGame_17 +label PongGame_16 push this 1 push local 0 call Ball.bounce 2 pop temp 0 - goto L14 -label L15 -label L14 + goto PongGame_14 +label PongGame_15 +label PongGame_14 push constant 0 return`; diff --git a/projects/src/samples/project_11/square/square.ts b/projects/src/samples/project_11/square/square.ts index 7237e93ea..454bc3425 100644 --- a/projects/src/samples/project_11/square/square.ts +++ b/projects/src/samples/project_11/square/square.ts @@ -1722,7 +1722,7 @@ function Square.incSize 0 lt and not - if-goto L1 + if-goto Square_1 push pointer 0 call Square.erase 1 pop temp 0 @@ -1733,9 +1733,9 @@ function Square.incSize 0 push pointer 0 call Square.draw 1 pop temp 0 - goto L0 -label L1 -label L0 + goto Square_0 +label Square_1 +label Square_0 push constant 0 return function Square.decSize 0 @@ -1745,7 +1745,7 @@ function Square.decSize 0 push constant 2 gt not - if-goto L3 + if-goto Square_3 push pointer 0 call Square.erase 1 pop temp 0 @@ -1756,9 +1756,9 @@ function Square.decSize 0 push pointer 0 call Square.draw 1 pop temp 0 - goto L2 -label L3 -label L2 + goto Square_2 +label Square_3 +label Square_2 push constant 0 return function Square.moveUp 0 @@ -1768,7 +1768,7 @@ function Square.moveUp 0 push constant 1 gt not - if-goto L5 + if-goto Square_5 push constant 0 call Screen.setColor 1 pop temp 0 @@ -1804,9 +1804,9 @@ function Square.moveUp 0 add call Screen.drawRectangle 4 pop temp 0 - goto L4 -label L5 -label L4 + goto Square_4 +label Square_5 +label Square_4 push constant 0 return function Square.moveDown 0 @@ -1818,7 +1818,7 @@ function Square.moveDown 0 push constant 254 lt not - if-goto L7 + if-goto Square_7 push constant 0 call Screen.setColor 1 pop temp 0 @@ -1854,9 +1854,9 @@ function Square.moveDown 0 add call Screen.drawRectangle 4 pop temp 0 - goto L6 -label L7 -label L6 + goto Square_6 +label Square_7 +label Square_6 push constant 0 return function Square.moveLeft 0 @@ -1866,7 +1866,7 @@ function Square.moveLeft 0 push constant 1 gt not - if-goto L9 + if-goto Square_9 push constant 0 call Screen.setColor 1 pop temp 0 @@ -1902,9 +1902,9 @@ function Square.moveLeft 0 add call Screen.drawRectangle 4 pop temp 0 - goto L8 -label L9 -label L8 + goto Square_8 +label Square_9 +label Square_8 push constant 0 return function Square.moveRight 0 @@ -1916,7 +1916,7 @@ function Square.moveRight 0 push constant 510 lt not - if-goto L11 + if-goto Square_11 push constant 0 call Screen.setColor 1 pop temp 0 @@ -1952,8 +1952,8 @@ function Square.moveRight 0 add call Screen.drawRectangle 4 pop temp 0 - goto L10 -label L11 -label L10 + goto Square_10 +label Square_11 +label Square_10 push constant 0 return`; diff --git a/projects/src/samples/project_11/square/square_game.ts b/projects/src/samples/project_11/square/square_game.ts index bf7c5848f..99a130e16 100644 --- a/projects/src/samples/project_11/square/square_game.ts +++ b/projects/src/samples/project_11/square/square_game.ts @@ -787,46 +787,46 @@ function SquareGame.moveSquare 0 push constant 1 eq not - if-goto L1 + if-goto SquareGame_1 push this 0 call Square.moveUp 1 pop temp 0 - goto L0 -label L1 -label L0 + goto SquareGame_0 +label SquareGame_1 +label SquareGame_0 push this 1 push constant 2 eq not - if-goto L3 + if-goto SquareGame_3 push this 0 call Square.moveDown 1 pop temp 0 - goto L2 -label L3 -label L2 + goto SquareGame_2 +label SquareGame_3 +label SquareGame_2 push this 1 push constant 3 eq not - if-goto L5 + if-goto SquareGame_5 push this 0 call Square.moveLeft 1 pop temp 0 - goto L4 -label L5 -label L4 + goto SquareGame_4 +label SquareGame_5 +label SquareGame_4 push this 1 push constant 4 eq not - if-goto L7 + if-goto SquareGame_7 push this 0 call Square.moveRight 1 pop temp 0 - goto L6 -label L7 -label L6 + goto SquareGame_6 +label SquareGame_7 +label SquareGame_6 push constant 5 call Sys.wait 1 pop temp 0 @@ -837,112 +837,112 @@ function SquareGame.run 2 pop pointer 0 push constant 0 pop local 1 -label L8 +label SquareGame_8 push local 1 not not - if-goto L9 -label L10 + if-goto SquareGame_9 +label SquareGame_10 push local 0 push constant 0 eq not - if-goto L11 + if-goto SquareGame_11 call Keyboard.keyPressed 0 pop local 0 push pointer 0 call SquareGame.moveSquare 1 pop temp 0 - goto L10 -label L11 + goto SquareGame_10 +label SquareGame_11 push local 0 push constant 81 eq not - if-goto L13 + if-goto SquareGame_13 push constant 1 neg pop local 1 - goto L12 -label L13 -label L12 + goto SquareGame_12 +label SquareGame_13 +label SquareGame_12 push local 0 push constant 90 eq not - if-goto L15 + if-goto SquareGame_15 push this 0 call Square.decSize 1 pop temp 0 - goto L14 -label L15 -label L14 + goto SquareGame_14 +label SquareGame_15 +label SquareGame_14 push local 0 push constant 88 eq not - if-goto L17 + if-goto SquareGame_17 push this 0 call Square.incSize 1 pop temp 0 - goto L16 -label L17 -label L16 + goto SquareGame_16 +label SquareGame_17 +label SquareGame_16 push local 0 push constant 131 eq not - if-goto L19 + if-goto SquareGame_19 push constant 1 pop this 1 - goto L18 -label L19 -label L18 + goto SquareGame_18 +label SquareGame_19 +label SquareGame_18 push local 0 push constant 133 eq not - if-goto L21 + if-goto SquareGame_21 push constant 2 pop this 1 - goto L20 -label L21 -label L20 + goto SquareGame_20 +label SquareGame_21 +label SquareGame_20 push local 0 push constant 130 eq not - if-goto L23 + if-goto SquareGame_23 push constant 3 pop this 1 - goto L22 -label L23 -label L22 + goto SquareGame_22 +label SquareGame_23 +label SquareGame_22 push local 0 push constant 132 eq not - if-goto L25 + if-goto SquareGame_25 push constant 4 pop this 1 - goto L24 -label L25 -label L24 -label L26 + goto SquareGame_24 +label SquareGame_25 +label SquareGame_24 +label SquareGame_26 push local 0 push constant 0 eq not not - if-goto L27 + if-goto SquareGame_27 call Keyboard.keyPressed 0 pop local 0 push pointer 0 call SquareGame.moveSquare 1 pop temp 0 - goto L26 -label L27 - goto L8 -label L9 + goto SquareGame_26 +label SquareGame_27 + goto SquareGame_8 +label SquareGame_9 push constant 0 return`; diff --git a/simulator/src/jack/compiler.test.ts b/simulator/src/jack/compiler.test.ts index 7671fcea8..19afc7224 100644 --- a/simulator/src/jack/compiler.test.ts +++ b/simulator/src/jack/compiler.test.ts @@ -81,6 +81,7 @@ describe("compiler", () => { ).statement; const compiler = new Compiler(); + compiler.className = "Main"; compiler.localSymbolTable = { condition: { type: "boolean", @@ -97,14 +98,14 @@ describe("compiler", () => { expect(compiler.output).toEqual([ "push local 0", "not", - "if-goto L1", + "if-goto Main_1", "push constant 4", "pop local 1", - "goto L0", - "label L1", + "goto Main_0", + "label Main_1", "push constant 5", "pop local 1", - "label L0", + "label Main_0", ]); }); diff --git a/simulator/src/jack/compiler.ts b/simulator/src/jack/compiler.ts index a364b6830..a0c58b688 100644 --- a/simulator/src/jack/compiler.ts +++ b/simulator/src/jack/compiler.ts @@ -207,7 +207,7 @@ export class Compiler { } getLabel() { - const label = `L${this.labelNum}`; + const label = `${this.className}_${this.labelNum}`; this.labelNum += 1; return label; }