def gen_get_or_create(cls, top, _): func = top.define_function('_internal/entity_ptr') func.preamble.add(i.PureInsn()) retvar = func.preamble.define(i.ReturnVarInsn(i.VarType.i32)) ct = func.create_compiletime() entry = func.create_block('entry') set_uid = func.create_block('set_uid') set_uid.set_is_function() ctblock = ct.create_block('ct_entry') uid_obj = top.preamble.add(cls.objective(), True, '__uid') sender = func.preamble.define(i.CreateSelector(i.SelectorType.SENDER)) self_uid = func.preamble.define( i.CreateEntityLocalAccess(uid_obj, sender)) next_uid = func.preamble.define( i.CreateEntityLocalAccess(uid_obj, top.lookup('_global_entity'))) ex = func.preamble.define(i.CreateExec()) # unless our uid is 1 or greater... ctblock.add(i.ExecUnlessVar(ex, self_uid, 1, None)) ct.run_and_return() entry.add(i.ExecRun(ex, set_uid)) entry.add(i.SetScore(retvar, self_uid)) entry.add(i.Return()) set_uid.add(i.SetScore(self_uid, next_uid)) set_uid.add(i.AddScore(next_uid, 1)) func.end() return func
def at_entity(self): block = self.sup.compiler.create_block('at_entity') block.set_is_function() as_block, sender = self.as_entity() ex = as_block.add(i.CreateExec(), True) as_block.add(i.ExecAtEntity(ex, sender)) as_block.add(i.ExecRun(ex, block)) return block
def at_entity(self): block = self.sup.compiler.create_block('at_entity') block.set_is_function() as_block, sender = self.as_entity() with self.sup.compiler.compiletime(): ex = self.sup.compiler.insn_def(i.CreateExec()) self.sup.compiler.add_insn(i.ExecAtEntity(ex, sender)) as_block.add(i.ExecRun(ex, block)) return block
def __is_empty(self, compiler, container, args): valvar = args[0].value.valvar() bool = compiler.type('bool') result = bool.allocate(compiler, 'empty') compiler.add_insn(i.SetScore(result, 0)) exec = compiler.define('empty_test', i.CreateExec()) set_empty = compiler.create_block('set_empty') set_empty.is_function = True set_empty.add(i.SetScore(result, 1)) with compiler.compiletime(): compiler.add_insn(i.ExecUnlessNBTVar(exec, valvar)) compiler.add_insn(i.ExecRun(exec, set_empty)) return Temporary(bool, result)
def as_entity(self, ptr): block = self.compiler.create_block('as_entity') block.set_is_function() sender = self.compiler.insn_def(i.CreateSelector( i.SelectorType.SENDER)) if ptr != self._current_sender: exec = self.compiler.insn_def(i.CreateExec()) sel = self.compiler.insn_def( i.CreateSelector(i.SelectorType.ALL_ENTITIES)) test_uid = self.compiler.insn_def( i.CreateEntityLocalAccess(self.uid_obj, sender)) self.compiler.add_insn(i.ExecAsEntity(exec, sel)) self.compiler.add_insn(i.ExecIfCmp(exec, test_uid, 'eq', ptr)) self.compiler.add_insn(i.ExecRun(exec, block)) else: self.compiler.add_insn(i.Call(block)) return block, sender # sender for convenience