Skip to content

Commit

Permalink
Generate the instructions from yaml file.
Browse files Browse the repository at this point in the history
  • Loading branch information
pluskid committed Jul 4, 2008
1 parent 0a211b4 commit d10eb34
Show file tree
Hide file tree
Showing 6 changed files with 363 additions and 140 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*.pyc
insns.py
iset.py
16 changes: 2 additions & 14 deletions codec.py
@@ -1,21 +1,9 @@
import iseq

class Instruction(object):
def __init__(self, opcode, data):
self.opcode = opcode
self.name = data[1]
self.length = data[0]
self.tag = data[3]

INSN_MAP = {}
for i in range(len(iseq.INSNS)):
insn = Instruction(i, iseq.INSNS[i])
INSN_MAP[insn.name] = insn
import iset

def inplace_encode(asm):
i = 0
while i < len(asm):
insn = INSN_MAP[asm[i]]
insn = iset.INSN_MAP[asm[i]]
asm[i] = insn.opcode
i += insn.length
return asm
Expand Down
124 changes: 0 additions & 124 deletions iseq.py

This file was deleted.

204 changes: 204 additions & 0 deletions iset.yml
@@ -0,0 +1,204 @@
tags:
- ctrl_flow
- ctx_switch

# Instructions
#
# The opcode is the index of each instruction. Stack before and after
# instruction is only for documenting purpose. The elements are
# specified from bottom to top. E.g:
#
# stack_before: [a, b]
# stack_after: [b-a]
#
# will be
#
# +---------+
# | b |
# +---------+ ==> +---------+
# | a | | b-a |
# +---------+ +---------+
#

instructions:
-
name: ret
tags: [ctx_switch, ctrl_flow]
desc: Return from a procedure.
operands: []
stack_before: [return_value]
stack_after: []
code: |
pass
-
name: call
tags: [ctx_switch, ctrl_flow]
desc: Call a procedure.
operands: []
stack_before: []
stack_after: []
code: |
pass
-
name: push_local
tags: []
desc: Push value of a local variable to operand stack.
operands: [local]
stack_before: []
stack_after: [value]
code: |
idx = get_param(ctx, 1)
loc = ctx.proc.locals[idx]
ctx.vm.stk_push(loc)
-
name: set_local
tags: []
desc: Pop the stack top and assign it to a local variable.
operands: [local]
stack_before: [value]
stack_after: []
code: |
idx = get_param(ctx, 1)
val = ctx.vm.stk_pop()
ctx.proc.locals[idx] = val
-
name: push_literal
tags: []
desc: Push a literal to operand stack.
operands: [literal]
stack_before: []
stack_after: [value]
code: |
idx = get_param(ctx, 1)
lit = ctx.proc.literals[idx]
ctx.vm.stk_push(lit)
-
name: push_0
tags: []
desc: Push 0 to operand stack.
operands: []
stack_before: []
stack_after: [0]
code: |
ctx.vm.stk_push(0)
-
name: push_1
tags: []
desc: Push 1 to operand stack.
operands: []
stack_before: []
stack_after: [0]
code: |
ctx.vm.stk_push(1)
-
name: dup
tags: []
desc: Duplicate the stack top object.
operands: []
stack_before: []
stack_after: [value]
code: |
ctx.vm.stk_push(ctx.vm.stk_top())
-
name: plus
tags: []
desc: Performan arithmetic +
operands: []
stack_before: [a, b]
stack_after: [b+a]
code: |
res = ctx.vm.stk_pop() + ctx.vm.stk_pop()
ctx.vm.stk_push(res)
-
name: minus
tags: []
desc: Performan arithmetic -
operands: []
stack_before: [a, b]
stack_after: [b-a]
code: |
res = ctx.vm.stk_pop() - ctx.vm.stk_pop()
ctx.vm.stk_push(res)
-
name: multiply
tags: []
desc: Performan arithmetic *
operands: []
stack_before: [a, b]
stack_after: [b*a]
code: |
res = ctx.vm.stk_pop() * ctx.vm.stk_pop()
ctx.vm.stk_push(res)
-
name: divide
tags: []
desc: Performance arithmetic /
operands: []
stack_before: [a, b]
stack_after: [b/a]
code: |
res = ctx.vm.stk_pop() / ctx.vm.stk_pop()
ctx.vm.stk_push(res)
-
name: equal
tags: []
desc: Test equality.
operands: []
stack_before: [a, b]
stack_after: [b==a]
code: |
res = (ctx.vm.stk_pop() == ctx.vm.stk_pop())
ctx.vm.stk_push(res)
-
name: goto
tags: [ctrl_flow]
desc: Unconditional jump.
operands: [ip]
stack_before: []
stack_after: []
code: |
ip = get_param(ctx, 1)
ctx.ip = ip
-
name: goto_if_true
tags: [ctrl_flow]
desc: Jump if the stack top is true.
operands: [ip]
stack_before: [condition]
stack_after: []
code: |
ip = get_param(ctx, 1)
cond = ctx.vm.stk_pop()
if cond is True:
ctx.ip = ip
else:
ctx.ip += $(insn_len)
-
name: goto_if_not_true
tags: [ctrl_flow]
desc: Jump if the stack top is not true.
operands: [ip]
stack_before: [condition]
stack_after: []
code: |
ip = get_param(ctx, 1)
cond = ctx.vm.stk_pop()
if cond is not True:
ctx.ip = ip
else:
ctx.ip += $(insn_len)

0 comments on commit d10eb34

Please sign in to comment.