Skip to content

Commit

Permalink
updated opcodes
Browse files Browse the repository at this point in the history
  • Loading branch information
zack-bitcoin committed Feb 2, 2017
1 parent c9d8f3a commit 24807cd
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 12 deletions.
159 changes: 154 additions & 5 deletions docs/opcodes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,156 @@
data is lists of binaries.


# Opcodes
## values opcodes
opcode | symbol | stack changes | comment
---| --- | --- | ---
0 | int | -- X | the next 32 bits = 4 bytes are put on the stack as a single binary.
2 | binary | N -- L | the next N * 8 bits are put on the stack as a single binary.


## other opcodes
opcode | symbol | stack changes | comment
---| --- | --- | ---
10 | print | ( Y -- X ) | prints the top element on stack
11 | crash | |code stops execution here. Whatever is on top of the stack is the final state.


## stack opcodes
opcode | symbol | stack changes | comment
--- | --- | --- | ---
20 | drop | X -- | will remove the top element on stack
21 | dup | X -- X X | duplicates the top element of the stack
22 | swap | A B -- B A| swaps the top two element of the stack
23 | tuck | a b c -- c a b |
24 | rot | a b c -- b c a |
25 | 2dup | a b -- a b a b |
26 | tuckn| X N -- | inserts X N-deeper into the stack.
27 | pickn| N -- X | grabs X from N-deep into the stack.


## r-stack opcodes
opcode | symbol | stack changes | comment
---| ---| --- | ---
30 | >r | V -- |
31 | r> | -- V | moves from return to stack
32 | r@ | -- V | copies from return to stack


## crypto opcodes
opcode | symbol | stack changes | comment
--- | --- | --- | ---
40 | hash | X -- <<Bytes:256>> |
41 | verify_sig | Sig Data Pub -- true/false |
42 | pub2addr | Pub -- Addr | This is difficult because we can't represent tuples. Maybe pinkcrypto:address needs to use lists instead


## arithmetic opcodes
Note about arithmetic opcodes:
they only works with 4-byte integers. Results are 4-byte integers. 32-bits. The integers are encoded so that FFFFFFFF is the highest integer and 00000000 is the lowest.

opcode | symbol | stack changes | comment
--- | --- | --- | ---
50 | + | X Y -- Z |
51 |- | X Y -- Z |
52 | * | X Y -- Z |
53 | / | X Y -- Z |
54 | > | X Y -- true/false X Y |
55 | < | X Y -- true/false X Y |
56 | ^ | X Y -- Z |
57 | rem| A B -- C | only works for integers.
58 | == | X Y -- true/false X Y |


## conditions opcodes

opcode | symbol | stack changes | comment
--- | --- | --- | ---
70 | if | | conditional statement
71 | else | | part of an switch conditional statement
72 | then | | part of switch conditional statement.


## logic opcodes
opcode | symbol | stack changes | comment
--- | --- | --- | ---
80 | not | true/false -- false/true |
81 | and | true/false true/false -- true/false | false is 0, true is any non-zero byte.
82 | or | true/false true/false -- true/false |
83 | xor | true/false true/false -- true/false |
84 | band| 4 12 -- 4 | if inputed binaries are different length, it returns a binary of the longer length
85 | bor | 4 8 -- 12 |
86 | bxor| 4 12 -- 8 |


## check state opcodes
Opcode not used anymore: questions. the root of the questions trie from the previous block. Used for crowdfunding the asking of questions. We don't need this because the oracle trie can be used to crowdfund the asking of questions.

opcode | symbol | stack changes | comment
--- | --- | --- | ---
90 | stack_size | -- Size |
91 | total_coins | -- TotalCoins |
92 | height | -- Height |
93 | slash | -- true/false | if this is part of a solo_stop transaction, then return 0. If it is part of a slash transaction, return 1
94 | gas | -- X |
95 | ram | -- X | tells how much space is left in ram.
96 | id2addr | ID -- Addr |
97 | many_vars | -- R | how many more variables are defined
98 | many_funs | -- F | how many functions are there defined
99 | oracle | -- R | the root of the oracle trie from the previous block.
100 | id_of_caller | -- ID | the ID of the person who published the code on-chain
101 | accounts | -- A | the root of the accounts trie from the previous block.
102 | channels | -- C | the root of the channels trie from the previous block.
103 | verify_merkle | Root Proof Value -- Value true/false |


## function opcodes
opcode | symbol | stack changes | comment
--- | --- | --- | ---
110 | : | | this starts the function declaration.
111 | ; | |This symbol ends a function declaration. example : square dup * ;
112 | recurse | |crash. this word should only be used in the definition of a word.
113 | call | | Use the binary at the top of the stack to look in our hashtable of defined words. If it exists call the code, otherwise crash.


## variables opcodes
opcode | symbol | stack changes | comment
--- | --- | --- | ---
120 | ! | X -- Y | only stores 32-bit integers
121 | @ | Y -- X |


## lists opcodes
opcode | symbol | stack changes | comment
--- | --- | --- | ---
130 | cons| X Y -- [X\|Y] |
131 | car | [X\|Y] -- X Y |
132 | nil | -- [] | this is the root of a list.
134 | ++ | X Y -- Z | appends 2 lists or 2 binaries. Cannot append a list to a binary.
135 | split | N Binary -- BinaryA BinaryB | Binary A has N*8 many bits. BinaryA appended to BinaryB makes Binary.
| | N List -- ListA ListB | ListA has N elements, listA appended to ListB makes List
136 | reverse | F -- G | only works on lists


The following are compiler macros that make it easier to program:

* ( a open parenthesis starts a multi-line comment block.

* ) a closed parenthesis ends the comment. Make sure to put spaces between the parenthesis and the other words.














data is lists of binaries.

opcode, symbol for compiled language, stack changes

Expand Down Expand Up @@ -149,7 +301,7 @@ only works with 4-byte integers. Results are 4-byte integers. 32-bits. The integ

# variables opcodes

120 ! % ( X Y -- ) % only stores 32-bit integers. stores the value X inside the variable Y.
120 ! % ( X Y -- )

121 @ ( Y -- X )

Expand All @@ -176,6 +328,3 @@ These are compiler macros to make it easier to program.

) a closed parenthesis ends the comment. Make sure to put spaces between the parenthesis and the other words.

or_die ( B -- ) if B is true, then does nothing. if B is false, then it crashes.

+! ( Number Name -- ) increments the variable Name by Number
2 changes: 1 addition & 1 deletion examples/first.scm
Original file line number Diff line number Diff line change
@@ -1 +1 @@
(* (+ 1 2) 5)
(* (+ (int 1) (int 2)) (int 5))
1 change: 0 additions & 1 deletion src/.#test_chalang.erl

This file was deleted.

40 changes: 35 additions & 5 deletions progress/compiler_lisp.erl → src/compiler_lisp.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@ doit(A) ->
%get macros
%apply macros
%check that each operator has the correct number of variables.
true = var_number_check(Tree),
var_number_check(Tree),
Tree2 = rpn(Tree),
Opcodes = to_opcodes(Tree2),
%switch to reverse polish notation.
%remove the parenthasis, and replace symbols with opcodes.
Opcodes.
Tree2.
w2o(<<"+">>) ->
{50, 2, 1};%{opcode, inputs, outputs}
w2o(<<"*">>) ->
{52, 2, 1}.
{52, 2, 1};
w2o(<<"int">>) ->%its an integer
{0, 1, 1};
w2o(X) -> {X, 0, 1}.

to_lists(Words) ->
case to_lists(Words, [], 0) of
{ok, X} -> X;
Expand Down Expand Up @@ -73,7 +77,33 @@ add_spaces(<<41:8, B/binary>>, Out) -> % )
add_spaces(B, <<Out/binary, 32:8, 41:8, 32:8>>);
add_spaces(<<X:8, B/binary >>, Out) ->
add_spaces(B, <<Out/binary, X:8>>).
var_number_check([]) -> true;
var_number_check([]) -> 0;
var_number_check([H|T]) ->
{Op, In, Out} = w2o(H),
{_Op, In, Out} = w2o(H),
In = var_number_check2(T),
Out.
var_number_check2([]) -> 0;
var_number_check2([H|T]) when is_list(H) ->
var_number_check(H) +
var_number_check2(T);
var_number_check2([H|T]) ->
1+var_number_check2(T).
rpn([]) -> [];
rpn([H|T]) ->
rpn2(T) ++ [H].
rpn2([]) -> [];
rpn2([H|T]) when is_list(H) ->
[rpn(H)|rpn2(T)].
to_opcodes(Tree) ->
List = flatten(Tree),
to_ops(List).
flatten([]) -> [];
flatten([H|T]) ->
flatten(H) ++ flatten(T);
flatten(X) -> [X].
to_ops([]) -> [];
to_ops([H|T]) ->
{Op, _, _} = w2o(H),
[Op|to_ops(T)].


0 comments on commit 24807cd

Please sign in to comment.