Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

moved python stuff to own folder

  • Loading branch information...
commit 84d4ef2ddc776b7ef2b406725e6d0b2456eea2a7 1 parent 5b3d267
@chrysn authored
View
0  tools/pyxample.py → examples/python/pyxample.py
File renamed without changes
View
2  tools/asm2bin → pysrc/asm2bin
@@ -2,7 +2,7 @@
import sys
-import asm
+from embedvm import asm
def main():
a = asm.ASM()
View
2  tools/bin2asm → pysrc/bin2asm
@@ -2,7 +2,7 @@
import sys
-import asm
+from embedvm import asm
def main():
a = asm.ASM()
View
4 tools/bin2py → pysrc/bin2py
@@ -1,8 +1,8 @@
#!/usr/bin/env python
import sys
-from asm import ASM
-import python_writer # monkey patching
+from embedvm.asm import ASM
+from embedvm import python_writer # monkey patching
def main():
a = ASM()
View
0  pysrc/embedvm/__init__.py
No changes.
View
2  tools/asm.py → pysrc/embedvm/asm.py
@@ -1,5 +1,5 @@
import ast
-import bytecode
+from . import bytecode
joining = lambda f: lambda self: "\n".join(f(self))
adding = lambda f: lambda self: sum(f(self), [])
View
0  tools/bytecode.py → pysrc/embedvm/bytecode.py
File renamed without changes
View
2  tools/python_writer.py → pysrc/embedvm/python_writer.py
@@ -1,4 +1,4 @@
-from bytecode import *
+from .bytecode import *
PushLocal.to_python = lambda self: ("lv.append(arguments[%d])" if self.sfa < 0 else "lv.append(lv[%d])")%self.sfa
PopLocal.to_python = lambda self: ("arguments[%d] = lv.pop()" if self.sfa < 0 else "lv[%d] = lv.pop()")%self.sfa
View
14 tools/py2bin → pysrc/py2bin
@@ -2,8 +2,8 @@
import sys
import ast
-from asm import joining
-import bytecode
+from embedvm.asm import joining
+from embedvm import bytecode
deduplicate = lambda iterable: reduce(lambda a, b: a if b in a else a+[b], iterable, [])
@@ -92,6 +92,8 @@ class Globals(object):
self.assigned.append(value)
self.named[attr] = value
value.pos = self.pos
+ if value.specified_pos is not None and value.specified_pos != value.pos:
+ raise Exception("Following forced memory alignment not yet supported")
self.pos += value.bytes
def get_initial_data(self):
@@ -108,8 +110,14 @@ class Globals(object):
self.gv = gv
def call(self, args, starargs, keywords, kwargs):
- if args or starargs or kwargs:
+ if starargs or kwargs:
raise Exception("Can't handle those arguments")
+ if len(args) not in (0, 1) or len(args) == 1 and not isinstance(args[0], ast.Num):
+ raise Exception("Can't handle those arguments")
+ if args:
+ self.specified_pos = raising_int(args[0].n)
+ else:
+ self.specified_pos = None
keywords_converted = {}
for k in keywords:
keywords_converted[k.arg] = ast.literal_eval(k.value)
View
1  tools/base_evm.py
View
125 tools/evm.py
@@ -1,125 +0,0 @@
-from bytecode import signext, assert_signexted
-
-class Globals(list):
- """Enhanced list of uint8 values that supports the access modes needed for
- the EVM. It also supports views on global arrays and variables.
-
- While write access to views on arrays can be implemented easily (like
- ``my_array = gv.array16(0); my_array[23] = 42``, a trick is employed to
- implement write access single variables (assigning to which would overwrite
- the binding): If something is assigned to a Globals, it is remembered in
- _known_view. On later assignments, instead of re-assigning, the previously
- assigned value's set function is called with the new value::
-
- >>> gv = Globals([0]*16)
- >>> gv.foo = gv.array8s(address=0)
- >>> gv.bar = gv.int8s(address=8)
- >>> gv.foo[7] = 10
- >>> gv.bar = 9
- """
-
- def __init__(self, *args):
- super(Globals, self).__init__(*args)
- self.__dict__['_known_view'] = {}
-
- def __setattr__(self, key, value):
- if key in self._known_view:
- self.__dict__['_known_view'][key].set(value)
- else:
- self.__dict__['_known_view'][key] = value
-
- def __getattr__(self, key):
- return self.__dict__['_known_view'][key].get()
-
- def get16(self, address):
- if len(self) < address+2:
- self.extend([0]*(address+2-len(self)))
- return signext((self[address]<<8) + self[address+1], 0xffff)
- def get8s(self, address):
- if len(self) < address+1:
- self.extend([0]*(address+1-len(self)))
- return signext(self[address], 0xff)
- def get8u(self, address):
- if len(self) < address+1:
- self.extend([0]*(address+1-len(self)))
- return self[address]
- def set16(self, address, value):
- if len(self) < address+2:
- self.extend([0]*(address+2-len(self)))
- assert_signexted(value, 0xffff)
- self[address:address+2] = divmod(value, 0x100)
- def set8s(self, address, value):
- if len(self) < address+1:
- self.extend([0]*(address+1-len(self)))
- assert_signexted(value, 0xff)
- self[address] = value%256
- def set8u(self, address, value):
- if len(self) < address+1:
- self.extend([0]*(address+1-len(self)))
- self[address] = value & 0xff
-
- array16 = lambda self, address=None, init=None, length=None: self.ArrayView16(self, address, init, length)
- array8u = lambda self, address=None, init=None, length=None: self.ArrayView8u(self, address, init, length)
- array8s = lambda self, address=None, init=None, length=None: self.ArrayView8s(self, address, init, length)
- int16 = lambda self, address=None, init=None: self.SingleView16(self, address, init)
- int8u = lambda self, address=None, init=None: self.SingleView8u(self, address, init)
- int8s = lambda self, address=None, init=None: self.SingleView8s(self, address, init)
-
- class View(object):
- def __init__(self, gv, address):
- self.gv = gv
- self.address = address if address is not None else len(gv)
-
- class SingleView(View):
- def __init__(self, gv, address, init_value):
- super(gv.SingleView, self).__init__(gv, address)
- if init_value is None:
- self.set(self.get()) # make sure gv is sized appropriately
- else:
- self.set(init_value)
-
- class SingleView8s(SingleView):
- get = lambda self: self.gv.get8s(self.address)
- set = lambda self, value: self.gv.set8s(self.address, value)
- class SingleView8u(SingleView):
- get = lambda self: self.gv.get8u(self.address)
- set = lambda self, value: self.gv.set8u(self.address, value)
- class SingleView16(SingleView):
- get = lambda self: self.gv.get16(self.address)
- set = lambda self, value: self.gv.set16(self.address, value)
-
- class ArrayView(View):
- def __init__(self, gv, address, init_value, length):
- super(gv.ArrayView, self).__init__(gv, address)
-
- if init_value is not None:
- for (i, x) in enumerate(init_value):
- self[i] = x
- if length is not None:
- for i in range(length):
- self[i] = self[i] # make sure gv is long enough
- # if neither init_value nor length are set, we are either in disassembled code or it is a null length array
-
- def get(self):
- return self
-
- class ArrayView8s(ArrayView):
- sizeofelement = 1
- __getitem__ = lambda self, index: self.gv.get8s(self.address + index)
- __setitem__ = lambda self, index, value: self.gv.set8s(self.address + index, value)
- class ArrayView8u(ArrayView):
- sizeofelement = 1
- __getitem__ = lambda self, index: self.gv.get8u(self.address + index)
- __setitem__ = lambda self, index, value: self.gv.set8u(self.address + index, value)
- class ArrayView16(ArrayView):
- sizeofelement = 2
- __getitem__ = lambda self, index: self.gv.get16(self.address + 2*index)
- __setitem__ = lambda self, index, value: self.gv.set16(self.address + 2*index, value)
-
-def userfunc(whichone, *argv):
- print "calling userfunc %d with arguments %s"%(whichone, argv)
- return 1234
-
- print "what should i return?"
- i = raw_input()
- return int(i) if i else 0
View
81 tools/example.evm
@@ -1,81 +0,0 @@
-/*
- * EmbedVM - Embedded Virtual Machine for uC Applications
- *
- * Copyright (C) 2011 Clifford Wolf <clifford@clifford.at>
- *
- * Permission to use, copy, modify, and/or distribute this software for any
- * purpose with or without fee is hereby granted, provided that the above
- * copyright notice and this permission notice appear in all copies.
- *
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
- *
- */
-
-
-$memaddr 0x0010
-
-global x;
-global y = 0x22;
-global z = 0x33;
-
-array8u array_8bit_unsigned[16];
-array8s array_8bit_signed[16] = { };
-array16 array_16bit[16] = { 1, 2, 3, 4 };
-
-function sum3(a,b,c)
-{
- return a + 2*b + 3*c;
-}
-
-function sum_loop()
-{
- local i, j;
- for (i=0; i<16; i=i+1)
- for (j=0; j<16; j=j+1) {
- array_16bit[i] = array_16bit[i] + array_8bit_unsigned[j];
- }
-}
-
-function init()
-{
- local i;
- for (i=0; i<16; i=i+1) {
- array_8bit_unsigned[i] = i;
- array_8bit_signed[i] = -i;
- array_16bit[i] = i * 500 * (i%2 == 0 ? +1 : -1);
- $uf2(i, array_16bit[i]);
- }
- x = 1;
- y = 2;
- z = 3;
-}
-
-function demo()
-{
- local i, sum;
-
- init();
- sum_loop();
-
- $uf1(1);
- $uf2(2, 3);
- $uf3(4, 5, 6);
- // $uf4($line, $addr);
- $uf5(sum3(1, 10, 100));
-
- for (i=0; i<16; i=i+1)
- sum = $uf6(sum, array_16bit[i]);
-
- $uf7($uf6(-sum) + 2914);
-
- // $uf0();
-}
-
-$trampoline 0x0000 demo
-
Please sign in to comment.
Something went wrong with that request. Please try again.