Commit Graph

6003 Commits

Author SHA1 Message Date
Kartik Agaram 6a28260f19 6003 2020-02-09 21:12:41 -08:00
Kartik Agaram 8fa733606c 6002 2020-02-09 21:03:04 -08:00
Kartik Agaram 3dad94263f 6001 2020-02-09 20:49:38 -08:00
Kartik Agaram 2c966386d1 6000 - clean up after no-local branches 2020-02-09 20:39:19 -08:00
Kartik Agaram 6c059c7ef3 5999
Fix CI. apps/survey was running out of space in the trace segment when
translating apps/mu.subx
2020-02-09 18:38:55 -08:00
Kartik Agaram 7b1786be40 5998 - redo code-generation for 'break'
I've been saying that we can convert this:

  {
    var x: int
    break-if-=
    ...
  }

..into this:

  {
    68/push 0/imm32
    {
      0f 84/jump-if-= break/disp32
      ...
    }
    81 0/subop/add %esp 4/imm32
  }

All subsequent instructions go into a nested block, so that they can be
easily skipped without skipping the stack cleanup.

However, I've been growing aware that this is a special case. Most of the
time we can't use this trick:
  for loops
  for non-local breaks
  for non-local loops

In most cases we need to figure out all the intervening variables on the
stack and emit code to clean them up.

And now it turns out even for local breaks like above, the trick doesn't
work. Consider what happens when there's a loop later in the block:

  {
    var x: int
    break-if-=
    ...
  }

If we emitted a nested block for the break, the local loop would become
non-local. So we replace one kind of state with another.

Easiest course of action is to just emit the exact same cleanup code for
all conditional branches.
2020-02-09 17:29:52 -08:00
Kartik Agaram ab6a6ed997 5997 - clean up after unconditional loops
Turns out we can't handle them like conditional loops.

This function to emit cleanup code for jumps is getting quite terrible.
I don't yet know what subsidiary abstractions it needs.
2020-02-09 16:49:04 -08:00
Kartik Agaram df65053a12 5996 2020-02-09 16:27:16 -08:00
Kartik Agaram b3891fbc0e 5995 2020-02-08 16:34:27 -08:00
Kartik Agaram b6a26f6ca5 5994 2020-02-08 16:33:43 -08:00
Kartik Agaram 0da12d59b7 5993 - support for unlabeled loop instructions
Now that we have the infrastructure for emitting cleanup blocks, the labeled
variants should be easy as well.
2020-02-08 16:23:23 -08:00
Kartik Agaram 0b636ffe72 5992 2020-02-07 00:14:17 -08:00
Kartik Agaram f3d054032d 5991 2020-02-07 00:02:09 -08:00
Kartik Agaram 3401742e55 5990 2020-02-06 21:15:34 -08:00
Kartik Agaram a3dfc8ba82 5989
Start pushing dummy vars for labels on the stack as we encounter them.
This won't affect cleanup code, but will make it easy to ensure that jumps
are well-structured.
2020-02-06 19:23:28 -08:00
Kartik Agaram 56d83f7915 5988
Clean up data structures and eliminate the notion of named blocks.

Named blocks still exist in the Mu language. But they get parsed into a
uniform block data structure, same as unamed blocks.
2020-02-06 16:39:56 -08:00
Kartik Agaram 83bf1291e0 5987 2020-02-06 16:32:42 -08:00
Kartik Agaram 52f5ce1fd3 5986 2020-02-06 16:29:40 -08:00
Kartik Agaram 8dbffb83a8 5985
Standardize on a single block name. This simplifies some code and will
also help in the next couple of steps.
2020-02-06 16:24:19 -08:00
Kartik Agaram 0e203a3120 5984 - start labeling all blocks
This will come in handy for the remaining cases where we need to clean
up locals on the stack:
  loop after var
  non-local break with vars in intervening blocks
  non-local loop with vars in intervening blocks
2020-02-05 22:35:28 -08:00
Kartik Agaram 9ee351f37f 5983 - fix an emulator bounds-check bug
It was possible for an instruction to write out of bounds of the memory
data structure. Most of the time this worked fine. However if the block
ever got resized and moved the out-of-bounds bytes no longer went along.
2020-02-05 14:57:52 -08:00
Kartik Agaram b9d666eff5 5982 - start putting block labels on the var stack
Before:
  we detected labels using a '$' at the start of an arg, and turned them
  into literals.

After:
  we put labels on the var stack and let the regular lookup of the var
  stack handle labels.

This adds complexity in one place and removes it from another. The crucial
benefit is that it allows us to store a block depth for each label. That
will come in handy later.

All this works only because of a salubrious coincidence: Mu labels are
always at the start of a block, and jumps always refer to the name at the
start of a block, even when the jump is in the forwards direction. So we
never see label uses before definitions.

Note on CI: this currently only works natively, not emulated.
2020-02-05 14:55:18 -08:00
Kartik Agaram 6b6b6851cc 5981 - decompose block cleanup into two traversals
Momentarily less efficient, but we will soon need the ability to emit cleanup
code without losing all our state.
2020-02-02 18:41:15 -08:00
Kartik Agaram 0566c88ff4 5980 2020-02-02 17:34:03 -08:00
Kartik Agaram f01db8bbc2 5979
Continuing to think about how to translate vars in a block when they're
followed by early exits. To clean up everything between a statement and
a target label, we need to know somehow the depth the target is defined
at.
2020-02-02 08:27:00 -08:00
Kartik Agaram d624175a2a 5978 2020-02-02 08:01:35 -08:00
Kartik Agaram 8099ed348d 5977 2020-02-02 07:55:19 -08:00
Kartik Agaram 14c4ab3828 5976 2020-02-02 00:28:44 -08:00
Kartik Agaram bed42be34f 5975 2020-02-02 00:23:49 -08:00
Kartik Agaram 84fd02c907 5974 - support for simple early exits
So far we only handle unlabeled break instructions correctly. That part
is elegance itself. But the rest will need more work:

a) For labeled breaks we need to insert code to unwind all intervening
blocks.
b) For unlabeled loops we need to insert code to unwind the current block
and then loop.
c) For labeled loops we need to insert code to unwind all intervening blocks
and then loop.

Is this even worth doing? I think so. It's pretty common for a conditional
block inside a loop to 'continue'. That requires looping to somewhere non-local.
2020-02-02 00:19:19 -08:00
Kartik Agaram bf3de55c3e 5973 2020-02-02 00:05:59 -08:00
Kartik Agaram 399d2972f2 5972 2020-02-01 22:56:51 -08:00
Kartik Agaram 2ea560deac 5971 - emit code with indentation
This is easy now that we're tracking block depths everywhere.
2020-02-01 22:55:30 -08:00
Kartik Agaram f0d3519a0c 5970 - support block-scoped variables 2020-02-01 17:05:59 -08:00
Kartik Agaram 819513fb53 5969 2020-02-01 12:21:48 -08:00
Kartik Agaram 924ed08aca 5968 2020-02-01 12:14:20 -08:00
Kartik Agaram 9977cfe53c 5967 2020-02-01 01:02:27 -08:00
Kartik Agaram aeac1e061d 5966 - document all supported Mu instructions 2020-01-31 18:55:37 -08:00
Kartik Agaram 4bb0b7e93f 5965 2020-01-31 16:17:04 -08:00
Kartik Agaram c2bec7a60f 5964 2020-01-30 01:26:13 -08:00
Kartik Agaram 21627e9d02 5963 2020-01-30 01:21:36 -08:00
Kartik Agaram 4caddd3bb3 5962 - string literals 2020-01-30 01:16:59 -08:00
Kartik Agaram c428664958 5961 2020-01-30 01:06:37 -08:00
Kartik Agaram 734fb226b1 5960 2020-01-30 00:38:37 -08:00
Kartik Agaram b18bea703c 5959 2020-01-30 00:21:24 -08:00
Kartik Agaram 4c0b4b4611 5958 2020-01-30 00:14:53 -08:00
Kartik Agaram 9eac92069d 5957 - bootstrap: stale checks for 2-byte opcodes 2020-01-30 00:02:53 -08:00
Kartik Agaram 19e7a418c9 5956 2020-01-29 23:57:45 -08:00
Kartik Agaram 65a1073150 5955 - error messages when translating Mu programs 2020-01-29 23:26:46 -08:00
Kartik Agaram ea62afb1da 5954 - 'factorial' working! 2020-01-29 23:17:36 -08:00