Skip to content

Commit

Permalink
Merge pull request #27 from Idein/namespace
Browse files Browse the repository at this point in the history
Support label namespace
  • Loading branch information
Terminus-IMRC authored Jan 10, 2020
2 parents ef551a6 + 66beeaa commit dee5b71
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 2 deletions.
79 changes: 79 additions & 0 deletions tests/test_labels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

import time
from videocore6.driver import Driver
from videocore6.assembler import qpu
import numpy as np

@qpu
def qpu_label_with_namespace(asm):

mov(r0, 0)

with namespace('ns1'):
b(R.test, cond = 'always')
nop()
nop()
nop()
add(r0, r0, 10)
L.test
add(r0, r0, 1)

with namespace('nested'):
b(R.test, cond = 'always')
nop()
nop()
nop()
add(r0, r0, 10)
L.test
add(r0, r0, 1)

with namespace('ns2'):
b(R.test, cond = 'always')
nop()
nop()
nop()
add(r0, r0, 10)
L.test
add(r0, r0, 1)

b(R.test, cond = 'always')
nop()
nop()
nop()
add(r0, r0, 10)
L.test
add(r0, r0, 1)

eidx(r1, sig = ldunifrf(rf2))
shl(r1, r1, 2)

mov(tmud, r0)
add(tmua, rf2, r1)
tmuwt()

nop(sig = thrsw)
nop(sig = thrsw)
nop()
nop()
nop(sig = thrsw)
nop()
nop()
nop()

def test_label_with_namespace():

with Driver() as drv:

code = drv.program(qpu_label_with_namespace)
data = drv.alloc(16, dtype = 'uint32')
unif = drv.alloc(1, dtype = 'uint32')

data[:] = 1234

unif[0] = data.addresses()[0]

start = time.time()
drv.execute(code, unif.addresses()[0])
end = time.time()

assert (data == 4).all()
25 changes: 23 additions & 2 deletions videocore6/assembler.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Assembly(list):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.labels = {}
self.label_name_spaces = []

def _gen_unused_label(self, label_format='{}'):
n = len(self.labels)
Expand All @@ -24,6 +25,25 @@ def _gen_unused_label(self, label_format='{}'):

return label_format.format(n)

def _gen_ns_label_name(self, name):
return '.'.join(self.label_name_spaces + [name])


class LabelNameSpace(object):
'Label namespace controller.'

def __init__(self, asm, name):
super(LabelNameSpace, self).__init__()
self.asm = asm
self.name = name

def __enter__(self):
self.asm.label_name_spaces.append(self.name)
return self

def __exit__(self, exception_type, exception_value, traceback):
self.asm.label_name_spaces.pop()


class Label(object):

Expand All @@ -33,7 +53,7 @@ def __init__(self, asm):
def __getattr__(self, name):
if name in self.asm.labels:
raise AssembleError(f'Label is duplicated: {name}')
self.asm.labels[name] = len(self.asm)
self.asm.labels[self.asm._gen_ns_label_name(name)] = len(self.asm)


class Reference(object):
Expand All @@ -43,7 +63,7 @@ def __init__(self, asm, name=None):
self.name = name

def __getattr__(self, name):
return Reference(self.asm, name)
return Reference(self.asm, self.asm._gen_ns_label_name(name))

def __int__(self):
return self.asm.labels[self.name]
Expand Down Expand Up @@ -1099,6 +1119,7 @@ def decorator(asm, *args, **kwargs):
g['b'] = functools.partial(Branch, asm, 'b')
g['link'] = Link()
g['raw'] = functools.partial(Raw, asm)
g['namespace'] = functools.partial(LabelNameSpace, asm)
for mul_op in MulALUOp.OPERATIONS.keys():
g[mul_op] = functools.partial(ALU, asm, mul_op)
for add_op in AddALUOp.OPERATIONS.keys():
Expand Down

0 comments on commit dee5b71

Please sign in to comment.