Decompilation

It is sometimes useful to inspect the internals of a word. There are two ways to do this:

  1. SEE
    decompiles a named word,
  2. DECOMPILE
    decompiles a given XT to screen.
 	
		: simple 1 2 + . ; 
		
		SEE simple
		
Running this program:
		[0]	Literal(1)
		[1]	Literal(2)
		[2]	+
		[3]	.

		ok
		
The listing shows the contents of the code section for the word
SIMPLE
. The numbers on the right correspond to each action compiled by Smojo into the word
SIMPLE
. The
+
word is a primitive word, which adds two numbers.
SEE
ing it shows that you can't break it down further:
		see +	
			
		+ ok
		


		The word 
.
is a compound word meaning that it is constructed out of primitives and literals. You can
SEE
it too:
		see . 	
			
		[0]	(.)
		[1]	BL
		[2]	(.)

		ok
		

Using DECOMPILE

You can similarly decompile any XT (since the XT contains the word's code section), with the word 
DECOMPILE
:
	
		' otherwise DECOMPILE
		
		[0]	Literal XT: _
		[1]	COMPILE,
		[2]	->

		ok
		

Understanding Decompilations

Some further notes on decompilations:
DecompilationWhat it means
Literal

This puts the literal, whose value is shown between the parentheses, onto the stack. Literal XTs are also indicated by Literal XT: ...

Primitive & Compound Words

The actual name of the primitive/compound word is shown. Compound words can be 
SEE
n to show their component words.

Jump

An Unconditional Jump that moves the program to the given line in that word. For example,

	
				Jump<21>
			
is an instruction that moves instruction to Line #21. Note: any jump past the end of the compound word will exit that word.

CJump

A Conditional Jump that moves the program to the given line in that word, if the top of the stack evaluates to false. For example,

	
				CJump<5>
				
is an instruction that moves instruction to Line #5, only if the top of the stack evaluates to false. If it's true, then the program proceeds to the next instruction immediately after the CJump. Note: any jump past the end of the compound word will exit that word.

This list isn't exhaustive, but it covers most of the decompilations you will see. There are other items like 
BLOCKS
and
DOES>
that will be explained in later sections.

Quiz

Question 1

The word 
SEE
is a compound word, which uses the primitive
DECOMPILE
. How could you prove this?



Question 2

Decompile the word 
EXIT
. Is it primitive or compound?



Question 3

Decompile this word:

	
		: xyz EXIT ; 
		
Explain the decompilation listing:
  1. What happened to the primitive word for
    EXIT
    ? Why isn't it in
    XYZ
    's code section? Hint: is
    EXIT
    immediate?
  2. Were the words
    :
    and
    ; compiled into the code section of 
    XYZ
    ? Why not? Hint: Is
    :
    fired before or after
    XYZ
    is defined? Is
    ;
    immediate?
  3. What kind of jumps are there in the decompilation? Can you explain what it does?



Question 4

Try this word:

	
			: three ( n -- )
				3 = if 
					"Three!" . 
				else 
					exit 
				then 
			;
		
What does
THREE
do? Explain every line the decompilation listing. Can
THREE
be simplified? Test out your simplified version if so.



Next: Stack Words II