about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--apps/mu.subx62
1 files changed, 49 insertions, 13 deletions
diff --git a/apps/mu.subx b/apps/mu.subx
index 7ece54ae..94070a4b 100644
--- a/apps/mu.subx
+++ b/apps/mu.subx
@@ -805,27 +805,31 @@ $emit-subx-block:end:
     5d/pop-to-ebp
     c3/return
 
-emit-subx-statement:  # out : (address buffered-file), stmt : (address statement), vars : (address variable), regs : (address array (address variable)), primitives : (address opcode-info), functions : (address function)
+emit-subx-statement:  # out : (address buffered-file), stmt : (address statement), vars : (address variable), primitives : (address opcode-info), functions : (address function)
     # . prologue
     55/push-ebp
     89/<- %ebp 4/r32/esp
     # . save registers
     50/push-eax
     51/push-ecx
-    # if statement doesn't match either a primitive or function, abort
+    # if stmt matches a primitive, emit it
     {
-      # if find-matching-function(primitives, statement) break
-      (find-matching-function *(ebp+0x18) *(ebp+0xc))
+      (find-matching-function *(ebp+0x14) *(ebp+0xc))
       3d/compare-eax-and 0/imm32
-      75/jump-if-not-equal break/disp8
-      # if find-matching-function(functions, statement) break
-      (find-matching-function *(ebp+0x20) *(ebp+0xc))
+      74/jump-if-equal break/disp8
+      (emit-subx-primitive *(ebx+8) *(ebx+0xc) %eax)
+      e9/jump $emit-subx-statement:end/disp32
+    }
+    # else if stmt matches a function, emit a call to it
+    {
+      (find-matching-function *(ebp+0x18) *(ebp+0xc))
       3d/compare-eax-and 0/imm32
-      75/jump-if-not-equal break/disp8
-      # abort
-      e9/jump $emit-subx-statement:abort/disp32
+      74/jump-if-equal break/disp8
+      (emit-subx-call *(ebx+8) *(ebx+0xc) %eax)
+      e9/jump $emit-subx-statement:end/disp32
     }
-    # emit code for stmt according to curr and vars
+    # else abort
+    e9/jump $emit-subx-statement:abort/disp32
 $emit-subx-statement:end:
     # . restore registers
     59/pop-to-ecx
@@ -847,6 +851,38 @@ $emit-subx-statement:abort:
     cd/syscall  0x80/imm8
     # never gets here
 
+emit-subx-primitive:  # out : (address buffered-file), stmt : (address statement), vars : (address variable), primitive : (address function)
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # . save registers
+    50/push-eax
+    51/push-ecx
+$emit-subx-primitive:end:
+    # . restore registers
+    59/pop-to-ecx
+    58/pop-to-eax
+    # . epilogue
+    89/<- %esp 5/r32/ebp
+    5d/pop-to-ebp
+    c3/return
+
+emit-subx-call:  # out : (address buffered-file), stmt : (address statement), vars : (address variable), callee : (address function)
+    # . prologue
+    55/push-ebp
+    89/<- %ebp 4/r32/esp
+    # . save registers
+    50/push-eax
+    51/push-ecx
+$emit-subx-call:end:
+    # . restore registers
+    59/pop-to-ecx
+    58/pop-to-eax
+    # . epilogue
+    89/<- %esp 5/r32/ebp
+    5d/pop-to-ebp
+    c3/return
+
 find-matching-function:  # functions : (address function), stmt : (address statement) -> result/eax : (address function)
     # . prologue
     55/push-ebp
@@ -951,7 +987,7 @@ test-emit-subx-statement-primitive:
     68/push "increment"/imm32/name
     89/<- %ebx 4/r32/esp
     # convert
-    (emit-subx-statement _test-output-buffered-file %edx %ecx 0 %ebx 0)
+    (emit-subx-statement _test-output-buffered-file %edx %ecx %ebx 0)
     (flush _test-output-buffered-file)
 #?     # dump _test-output-stream {{{
 #?     (write 2 "^")
@@ -1021,7 +1057,7 @@ test-emit-subx-statement-function-call:
     68/push "f"/imm32/name
     89/<- %ebx 4/r32/esp
     # convert
-    (emit-subx-statement _test-output-buffered-file %edx %ecx 0 0 %ebx)
+    (emit-subx-statement _test-output-buffered-file %edx %ecx 0 %ebx)
     (flush _test-output-buffered-file)
 #?     # dump _test-output-stream {{{
 #?     (write 2 "^")
44 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