Browse code

Implemented local templated labels

neauoire authored on 11/03/2021 23:47:28
Showing 8 changed files
... ...
@@ -27,6 +27,7 @@ evaluxn(u, u->vframe); /* Each frame
27 27
 ### Define
28 28
 
29 29
 - `@label`, assign the current address to a label.
30
+- `$label`, assign the current address to a local label.
30 31
 - `;variable 2`, assign an address to a label automatically.
31 32
 - `:const 1a2b`, assign an address to a label manually.
32 33
 - `&macro { x 2 y 2 }`, define a macro named `macro`.
... ...
@@ -75,16 +76,16 @@ BRK
75 76
 
76 77
 @print-label ( text )
77 78
 	
78
-	NOP
79
-	( send ) DUP2 LDR =CNSL.char
80
-	( incr ) #0001 ADD2
81
-	( loop ) DUP2 LDR #00 NEQ ^print-label MUL JMPS 
79
+	$loop NOP
80
+		( send ) DUP2 LDR =CNSL.char
81
+		( incr ) #0001 ADD2
82
+		( loop ) DUP2 LDR #00 NEQ ^$loop MUL JMPS 
82 83
 	POP2
83 84
 
84
-RTS                 
85
+RTS    
85 86
 
86
-@text1 [ Hello 20 World 0a00 ] ( text with linebreak and null bytes )
87
-@text2 [ Welcome 20 to 20 UxnVM 0a00 ]
87
+@text1 [ Welcome 20 to 20 UxnVM 0a00 ]
88
+@text2 [ Hello 20 World 0a00 ] 
88 89
 
89 90
 |c000 @FRAME
90 91
 |d000 @ERROR 
... ...
@@ -167,6 +167,15 @@ findopcode(char *s)
167 167
 	return 0;
168 168
 }
169 169
 
170
+char *
171
+sublabel(char *src, char *scope, char *name)
172
+{
173
+	scpy(scope, src, 64);
174
+	scpy("-", src + slen(src), 64);
175
+	scpy(name, src + slen(src), 64);
176
+	return src;
177
+}
178
+
170 179
 #pragma mark - Parser
171 180
 
172 181
 int
... ...
@@ -274,7 +283,7 @@ pass1(FILE *f)
274 283
 {
275 284
 	int ccmnt = 0, cbits = 0;
276 285
 	Uint16 addr = 0;
277
-	char w[64];
286
+	char w[64], scope[64], subw[64];
278 287
 	printf("Pass 1\n");
279 288
 	while(fscanf(f, "%s", w) == 1) {
280 289
 		if(skipblock(w, &ccmnt, '(', ')')) continue;
... ...
@@ -288,6 +297,10 @@ pass1(FILE *f)
288 297
 		} else if(w[0] == '@') {
289 298
 			if(!makelabel(w + 1, addr, 0, NULL))
290 299
 				return error("Pass1 failed", w);
300
+			scpy(w + 1, scope, 64);
301
+		} else if(w[0] == '$') {
302
+			if(!makelabel(sublabel(subw, scope, w + 1), addr, 0, NULL))
303
+				return error("Pass1 failed", w);
291 304
 		} else if(w[0] == ';') {
292 305
 			if(!makevariable(w + 1, &addr, f))
293 306
 				return error("Pass1 failed", w);
... ...
@@ -308,8 +321,6 @@ pass1(FILE *f)
308 321
 				break;
309 322
 			case '=': addr += 4; break; /* STR helper (lit addr-hb addr-lb str) */
310 323
 			case '~': addr += 4; break; /* LDR helper (lit addr-hb addr-lb ldr) */
311
-			case '$': addr += 4; break; /* JSR helper (lit addr-hb addr-lb jsr) */
312
-			case '/': addr += 4; break; /* JMP helper (lit addr-hb addr-lb jmp) */
313 324
 			case ',': addr += 3; break;
314 325
 			case '.': addr += 2; break;
315 326
 			case '^': addr += 2; break; /* Relative jump: lit addr-offset */
... ...
@@ -328,13 +339,21 @@ int
328 339
 pass2(FILE *f)
329 340
 {
330 341
 	int ccmnt = 0, cbits = 0, cmacro = 0;
331
-	char w[64];
342
+	char w[64], scope[64], subw[64];
332 343
 	printf("Pass 2\n");
333 344
 	while(fscanf(f, "%s", w) == 1) {
334 345
 		Uint8 op = 0;
335 346
 		Label *l;
336
-		if(w[0] == '@') continue;
337 347
 		if(w[0] == '&') continue;
348
+		if(w[0] == '$') continue;
349
+		if(w[0] == '@') {
350
+			scpy(w + 1, scope, 64);
351
+			continue;
352
+		}
353
+		if(w[1] == '$') {
354
+			sublabel(subw, scope, w + 2);
355
+			scpy(subw, w + 1, 64);
356
+		}
338 357
 		if(skipblock(w, &ccmnt, '(', ')')) continue;
339 358
 		if(skipblock(w, &cmacro, '{', '}')) continue;
340 359
 		/* clang-format off */
... ...
@@ -357,8 +376,6 @@ pass2(FILE *f)
357 376
 		else if(w[0] == ',' && (l = findlabel(w + 1))) { pushshort(findlabeladdr(w + 1), 1); l->refs++; }
358 377
 		else if(w[0] == '=' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w + 1) == 2 ? "STR2" : "STR"), 0); l->refs++;}
359 378
 		else if(w[0] == '~' && (l = findlabel(w + 1)) && l->len){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode(findlabellen(w + 1) == 2 ? "LDR2" : "LDR"), 0); l->refs++;}
360
-		else if(w[0] == '/' && (l = findlabel(w + 1))){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode("JMP2"), 0); l->refs++;}
361
-		else if(w[0] == '$' && (l = findlabel(w + 1))){ pushshort(findlabeladdr(w + 1), 1); pushbyte(findopcode("JSR2"), 0); l->refs++;}
362 379
 		else if(w[0] == '#' && sihx(w + 1) && slen(w + 1) == 2) pushbyte(shex(w + 1), 1); 
363 380
 		else if(w[0] == '#' && sihx(w + 1) && slen(w + 1) == 4) pushshort(shex(w + 1), 1);
364 381
 		else if(w[0] == '+' && sihx(w + 1) && slen(w + 1) == 2) pushbyte((Sint8)shex(w + 1), 1);
... ...
@@ -30,6 +30,9 @@ contexts:
30 30
     - match: '\&(\S+)\s?'
31 31
       scope: string.control
32 32
       pop: true
33
+    - match: '\$(\S+)\s?'
34
+      scope: string.control
35
+      pop: true
33 36
 
34 37
     # Special
35 38
 
... ...
@@ -4,8 +4,8 @@
4 4
 
5 5
 |0100 @RESET 
6 6
 	
7
-	,text1 $print-label
8
-	,text2 $print-label
7
+	,text1 ,print-label JSR2
8
+	,text2 ,print-label JSR2
9 9
 	#ab =CNSL.byte
10 10
 	#cdef =CNSL.short
11 11
 
... ...
@@ -13,16 +13,14 @@ BRK
13 13
 
14 14
 @print-label ( text )
15 15
 	
16
-	@print-label-loop NOP
16
+	$loop NOP
17 17
 		( send ) DUP2 LDR =CNSL.char
18 18
 		( incr ) #0001 ADD2
19
-		( loop ) DUP2 LDR #00 NEQ ^print-label-loop MUL JMPS 
19
+		( loop ) DUP2 LDR #00 NEQ ^$loop MUL JMPS 
20 20
 	POP2
21 21
 
22 22
 RTS    
23 23
 
24
-( store text in memory )
25
-
26 24
 @text1 [ Welcome 20 to 20 UxnVM 0a00 ]
27 25
 @text2 [ Hello 20 World 0a00 ] 
28 26
 
29 27
similarity index 92%
30 28
rename from projects/examples/dev.key.usm
31 29
rename to projects/examples/dev.keys.usm
... ...
@@ -65,45 +65,45 @@ RTS
65 65
 
66 66
 	=dev/sprite.addr =color =rect.y2 =rect.x2 DUP2 =dev/sprite.y =rect.y1 DUP2 =dev/sprite.x =rect.x1
67 67
 
68
-	@tile-rect-ver
68
+	$ver
69 69
 		~rect.x1 =dev/sprite.x
70
-		@tile-rect-hor
70
+		$hor
71 71
 			( draw ) ~color =dev/sprite.color
72 72
 			( incr ) ~dev/sprite.x #0008 ADD2 =dev/sprite.x
73
-			,tile-rect-hor ~dev/sprite.x ~rect.x2 LTH2 JMP2? POP2
73
+			,$hor ~dev/sprite.x ~rect.x2 LTH2 JMP2? POP2
74 74
 		( incr ) ~dev/sprite.y #0008 ADD2 =dev/sprite.y
75
-		,tile-rect-ver ~dev/sprite.y ~rect.y2 LTH2 JMP2? POP2
75
+		,$ver ~dev/sprite.y ~rect.y2 LTH2 JMP2? POP2
76 76
 
77 77
 RTS
78 78
 
79 79
 @fill-rect ( x1 y1 x2 y2 color )
80 80
 
81 81
 	( load ) =color =rect.y2 =rect.x2 DUP2 =dev/screen.y =rect.y1 DUP2 =dev/screen.x =rect.x1
82
-	@fill-rect-ver
82
+	$ver
83 83
 		~rect.x1 =dev/screen.x
84
-		@fill-rect-hor
84
+		$hor
85 85
 			( draw ) ~color =dev/screen.color
86 86
 			( incr ) ~dev/screen.x #0001 ADD2 =dev/screen.x
87
-			,fill-rect-hor ~dev/screen.x ~rect.x2 LTH2 JMP2? POP2
87
+			,$hor ~dev/screen.x ~rect.x2 LTH2 JMP2? POP2
88 88
 		( incr ) ~dev/screen.y #0001 ADD2 =dev/screen.y
89
-		,fill-rect-ver ~dev/screen.y ~rect.y2 LTH2 JMP2? POP2
89
+		,$ver ~dev/screen.y ~rect.y2 LTH2 JMP2? POP2
90 90
 
91 91
 RTS
92 92
 
93 93
 @line-rect ( x1 y1 x2 y2 color )
94 94
 
95 95
 	( load ) =color =rect.y2 =rect.x2 DUP2 =dev/screen.y =rect.y1 DUP2 =dev/screen.x =rect.x1
96
-	@line-rect-hor
96
+	$hor
97 97
 		( incr ) ~dev/screen.x #0001 ADD2 =dev/screen.x
98 98
 		( draw ) ~rect.y1 =dev/screen.y ~color =dev/screen.color
99 99
 		( draw ) ~rect.y2 =dev/screen.y ~color =dev/screen.color
100
-		,line-rect-hor ~dev/screen.x ~rect.x2 LTH2 JMP2? POP2
100
+		,$hor ~dev/screen.x ~rect.x2 LTH2 JMP2? POP2
101 101
 	~rect.y1 =dev/screen.y
102
-	@line-rect-ver
102
+	$ver
103 103
 		( draw ) ~rect.x1 =dev/screen.x ~color =dev/screen.color
104 104
 		( draw ) ~rect.x2 =dev/screen.x ~color =dev/screen.color
105 105
 		( incr ) ~dev/screen.y #0001 ADD2 =dev/screen.y
106
-		,line-rect-ver ~dev/screen.y ~rect.y2 #0001 ADD2 LTH2 JMP2? POP2
106
+		,$ver ~dev/screen.y ~rect.y2 #0001 ADD2 LTH2 JMP2? POP2
107 107
 
108 108
 RTS
109 109
 
... ...
@@ -111,20 +111,17 @@ RTS
111 111
 	
112 112
 	( load ) =textarea.addr =textarea.color =dev/sprite.y =dev/sprite.x 
113 113
 	~textarea.addr
114
-	@draw-textarea-left-loop
114
+	$loop
115 115
 		( draw ) DUP2 LDR #00 SWP #0008 MUL2 ,font ADD2 =dev/sprite.addr ~textarea.color =dev/sprite.color
116
-
117 116
 		( detect linebreaks )
118
-		DUP2 LDR #0d NEQ ,no-return ROT JMP2? POP2
117
+		DUP2 LDR #0d NEQ ,$no-return ROT JMP2? POP2
119 118
 			~textarea.x1 =dev/sprite.x 
120 119
 			( incr ) ~dev/sprite.y #0008 ADD2 =dev/sprite.y
121 120
 			( decr ) ~dev/sprite.x #0008 SUB2 =dev/sprite.x
122
-		@no-return
123
-
121
+		$no-return
124 122
 		( incr ) #0001 ADD2
125 123
 		( incr ) ~dev/sprite.x #0008 ADD2 =dev/sprite.x
126
-
127
-		DUP2 LDR #00 NEQ ,draw-textarea-left-loop ROT JMP2? POP2
124
+		DUP2 LDR #00 NEQ ,$loop ROT JMP2? POP2
128 125
 	POP2
129 126
 
130 127
 RTS
... ...
@@ -129,11 +129,11 @@ RTS
129 129
 	
130 130
 	( load ) =label.addr =label.color =dev/sprite.y =dev/sprite.x 
131 131
 	~label.addr
132
-	@draw-label-loop
132
+	$loop
133 133
 		( draw ) DUP2 LDR #00 SWP #0008 MUL2 ,font ADD2 =dev/sprite.addr ~label.color =dev/sprite.color
134 134
 		( incr ) #0001 ADD2
135 135
 		( incr ) ~dev/sprite.x #0008 ADD2 =dev/sprite.x
136
-		DUP2 #0001 ADD2  LDR #00 NEQ ,draw-label-loop ROT JMP2? POP2
136
+		DUP2 #0001 ADD2  LDR #00 NEQ ,$loop ROT JMP2? POP2
137 137
 	POP2
138 138
 
139 139
 RTS
... ...
@@ -270,7 +270,7 @@ RTS
270 270
 	( guides )
271 271
 
272 272
 	#00 =i ,font_hex =SPRT.addr
273
-	@draw-bankview-guides
273
+	$guides
274 274
 		~bankview.x #0010 SUB2 =SPRT.x
275 275
 		~bankview.y #00 ~i #08 MUL ADD2 =SPRT.y
276 276
 		( draw ) #02 =SPRT.color
... ...
@@ -279,28 +279,28 @@ RTS
279 279
 		( draw ) #02 =SPRT.color
280 280
 		~SPRT.addr #0008 ADD2 =SPRT.addr
281 281
 		( incr ) ~i #01 ADD =i
282
-		,draw-bankview-guides ~i #10 LTH JMP2? POP2
282
+		,$guides ~i #10 LTH JMP2? POP2
283 283
 
284 284
 	( body )
285 285
 
286 286
 	~bankview.x =SPRT.x ~bankview.y =SPRT.y
287 287
 	#00 =pt.x #00 =pt.y ~bankview.addr =SPRT.addr
288
-	@draw-bankview-tiles-ver
288
+	$ver
289 289
 		#00 =pt.x
290 290
 		~bankview.x =SPRT.x
291
-		@draw-bankview-tiles-hor
291
+		$hor
292 292
 			( draw ) #01 =SPRT.color
293
-			,no-highlight ~SPRT.addr ~tileview.addr LTH2 JMP2? POP2
294
-				,no-highlight ~SPRT.addr ~tileview.addr #0018 ADD2 GTH2 JMP2? POP2
293
+			,$no-highlight ~SPRT.addr ~tileview.addr LTH2 JMP2? POP2
294
+			,$no-highlight ~SPRT.addr ~tileview.addr #0018 ADD2 GTH2 JMP2? POP2
295 295
 				( draw ) #0c =SPRT.color
296
-			@no-highlight
296
+			$no-highlight
297 297
 			( incr ) ~SPRT.x #0008 ADD2 =SPRT.x
298 298
 			( incr ) ~SPRT.addr #0008 ADD2 =SPRT.addr
299 299
 			( incr ) ~pt.x #01 ADD =pt.x
300
-			,draw-bankview-tiles-hor ~pt.x #10 LTH JMP2? POP2
300
+			,$hor ~pt.x #10 LTH JMP2? POP2
301 301
 		( incr ) ~pt.y #01 ADD =pt.y
302 302
 		( incr ) ~SPRT.y #0008 ADD2 =SPRT.y
303
-		,draw-bankview-tiles-ver ~pt.y #10 LTH JMP2? POP2
303
+		,$ver ~pt.y #10 LTH JMP2? POP2
304 304
 
305 305
 RTS
306 306
 
... ...
@@ -344,18 +344,18 @@ RTS
344 344
 	( line hor )
345 345
 	~tileview.y #003f ADD2 =SCRN.y
346 346
 	~tileview.x =SCRN.x
347
-	@draw-hor
347
+	$line-hor
348 348
 		( draw ) #03 =SCRN.color
349 349
 		( incr ) ~SCRN.x #0002 ADD2 =SCRN.x
350
-	~SCRN.x ~tileview.x #0082 ADD2 LTH2 ,draw-hor ROT JMP2? POP2
350
+	~SCRN.x ~tileview.x #0082 ADD2 LTH2 ,$line-hor ROT JMP2? POP2
351 351
 
352 352
 	( line ver )
353 353
 	~tileview.y =SCRN.y
354 354
 	~tileview.x #003f ADD2 =SCRN.x
355
-	@draw-ver
355
+	$line-ver
356 356
 		( draw ) #03 =SCRN.color
357 357
 		( incr ) ~SCRN.y #0002 ADD2 =SCRN.y
358
-	~SCRN.y ~tileview.y #0081 ADD2 LTH2 ,draw-ver ROT JMP2? POP2
358
+	~SCRN.y ~tileview.y #0081 ADD2 LTH2 ,$line-ver ROT JMP2? POP2
359 359
 
360 360
 	( rewind ) ~tileview.addr #0018 SUB2 =tileview.addr
361 361
 
... ...
@@ -363,7 +363,7 @@ RTS
363 363
 
364 364
 	~tileview.y #0018 ADD2 =SPRT.y
365 365
 	#00 =i
366
-	@draw-tileview-bytes
366
+	$bytes
367 367
 		~tileview.x #0088 ADD2 =SPRT.x
368 368
 		,font_hex #00 ~tileview.addr #00 ~i ADD2 LDR #f0 AND #04 ROR #08 MUL ADD2 =SPRT.addr
369 369
 		( draw ) #02 =SPRT.color
... ...
@@ -372,7 +372,7 @@ RTS
372 372
 		( draw ) #02 =SPRT.color
373 373
 		( incr ) ~i #01 ADD =i
374 374
 		( incr ) ~SPRT.y #0008 ADD2 =SPRT.y
375
-	,draw-tileview-bytes ~i #08 LTH JMP2? POP2
375
+	,$bytes ~i #08 LTH JMP2? POP2
376 376
 
377 377
 	( operations )
378 378
 
... ...
@@ -387,27 +387,27 @@ RTS
387 387
 	~tileview.y =SPRT.y
388 388
 	#00 =pt.x #00 =pt.y ~tileview.addr =SPRT.addr
389 389
 
390
-	@draw-tileview-tiles-ver
390
+	$tiles-ver
391 391
 		#00 =pt.x
392 392
 		~tileview.x #0088 ADD2 =SPRT.x
393
-		@draw-tileview-tiles-hor
393
+		$tiles-hor
394 394
 			( draw ) #03 =SPRT.color
395 395
 			( incr ) ~SPRT.x #0008 ADD2 =SPRT.x
396 396
 			( incr ) ~SPRT.addr #0008 ADD2 =SPRT.addr
397 397
 			( incr ) ~pt.x #01 ADD =pt.x
398
-			,draw-tileview-tiles-hor ~pt.x #02 LTH JMP2? POP2
398
+			,$tiles-hor ~pt.x #02 LTH JMP2? POP2
399 399
 		( incr ) ~pt.y #01 ADD =pt.y
400 400
 		( incr ) ~SPRT.y #0008 ADD2 =SPRT.y
401
-		,draw-tileview-tiles-ver ~pt.y #02 LTH JMP2? POP2
401
+		,$tiles-ver ~pt.y #02 LTH JMP2? POP2
402 402
 
403 403
 RTS
404 404
 
405 405
 @draw-tileview-icn
406 406
 
407 407
 	#00 =pt.x #00 =pt.y 
408
-	@redraw-ver
408
+	$ver
409 409
 		#00 =pt.x
410
-		@redraw-hor
410
+		$hor
411 411
 			( get bit )
412 412
 			,blank_icn #00
413 413
 			~tileview.addr #00 ~pt.y ADD2 LDR #07 ~pt.x SUB ROR #01 AND ( get bit )
... ...
@@ -415,11 +415,11 @@ RTS
415 415
 			( draw ) #01 =SPRT.color
416 416
 			( incr ) ~SPRT.x #0008 ADD2 =SPRT.x
417 417
 			( incr ) ~pt.x #01 ADD =pt.x
418
-			,redraw-hor ~pt.x #08 LTH JMP2? POP2
418
+			,$hor ~pt.x #08 LTH JMP2? POP2
419 419
 		( incr ) ~SPRT.y #0008 ADD2 =SPRT.y
420 420
 		( incr ) ~pt.y #01 ADD =pt.y
421 421
 		~SPRT.x #0040 SUB2 =SPRT.x
422
-		,redraw-ver ~pt.y #08 LTH JMP2? POP2
422
+		,$ver ~pt.y #08 LTH JMP2? POP2
423 423
 
424 424
 RTS
425 425
 
... ...
@@ -470,17 +470,17 @@ RTS
470 470
 @line-rect ( x1 y1 x2 y2 color )
471 471
 
472 472
 	( load ) =color =rect.y2 =rect.x2 DUP2 =SCRN.y =rect.y1 DUP2 =SCRN.x =rect.x1
473
-	@line-rect-hor NOP
473
+	$hor NOP
474 474
 		( incr ) ~SCRN.x #0001 ADD2 =SCRN.x
475 475
 		( draw ) ~rect.y1 =SCRN.y ~color =SCRN.color
476 476
 		( draw ) ~rect.y2 =SCRN.y ~color =SCRN.color
477
-		~SCRN.x ~rect.x2 LTH2 ^line-rect-hor MUL JMPS
477
+		~SCRN.x ~rect.x2 LTH2 ^$hor MUL JMPS
478 478
 	~rect.y1 =SCRN.y
479
-	@line-rect-ver NOP
479
+	$ver NOP
480 480
 		( draw ) ~rect.x1 =SCRN.x ~color =SCRN.color
481 481
 		( draw ) ~rect.x2 =SCRN.x ~color =SCRN.color
482 482
 		( incr ) ~SCRN.y #0001 ADD2 =SCRN.y
483
-		~SCRN.y ~rect.y2 #0001 ADD2 LTH2 ^line-rect-ver MUL JMPS
483
+		~SCRN.y ~rect.y2 #0001 ADD2 LTH2 ^$ver MUL JMPS
484 484
 
485 485
 RTS
486 486
 
... ...
@@ -7,22 +7,24 @@
7 7
 |0100 @RESET 
8 8
 	
9 9
 	( type: padded muljmp )
10
-
11
-	@loop1 NOP
10
+	@part1
11
+	$loop NOP
12 12
 		~a #01 ADD =a
13
-		~a #d0 LTH ^loop1 MUL JMPS
13
+		~a #d0 LTH ^$loop MUL JMPS
14 14
 
15 15
 	( type: jmppop )
16 16
 
17
-	@loop2
17
+	@part2
18
+	$loop
18 19
 		~b #01 ADD =b
19
-		,loop2 ~b #d0 LTH JMP2? POP2
20
+		,$loop ~b #d0 LTH JMP2? POP2
20 21
 
21 22
 	( type: padded jmppop )
22 23
 
23
-	@loop3 NOP
24
+	@part3
25
+	$loop NOP
24 26
 		~c #01 ADD =c
25
-		~c #d0 LTH ^loop3 SWP JMPS? POP
27
+		~c #d0 LTH ^$loop SWP JMPS? POP
26 28
 
27 29
 	~a =dev/console.byte
28 30
 	~b =dev/console.byte