CALL, CALL_MAP, BUILD_MAP, NAMEDARG_EXTRACT, NAMEDARG_EXTRACT_OPTIONAL, JUMP, ) = makeEnum( "SmallCaps op", [ s.strip() for s in """ dup rot pop swap assignGlobal assignFrame assignLocal bind bindFinalSlot bindVarSlot slotGlobal slotFrame slotLocal nounGlobal nounFrame nounLocal bindingGlobal bindingFrame bindingLocal listPatt literal bindObject scope ejector try unwind endHandler branch call callMap buildMap namedArgExtract namedArgExtractOptional jump """.split() ], ) ops = { "DUP": DUP, "ROT": ROT, "POP": POP, "SWAP": SWAP,
import weakref from typhon.atoms import getAtom from typhon.autohelp import autohelp, method from typhon.enum import makeEnum from typhon.errors import Ejecting, UserException, userError from typhon.log import log from typhon.objects.auditors import deepFrozenStamp, selfless from typhon.objects.constants import NullObject from typhon.objects.ejectors import Ejector from typhon.objects.root import Object, audited from typhon.vats import currentVat BROKEN, EVENTUAL, NEAR = makeEnum(u"RefState", u"broken eventual near".split()) RESOLVE_1 = getAtom(u"resolve", 1) RESOLVE_2 = getAtom(u"resolve", 2) RUN_1 = getAtom(u"run", 1) _WHENBROKEN_1 = getAtom(u"_whenBroken", 1) _WHENMORERESOLVED_1 = getAtom(u"_whenMoreResolved", 1) def makePromise(guard=None): vat = currentVat.get() buf = MessageBuffer(vat) sref = SwitchableRef(BufferingRef(buf)) return sref, LocalResolver(sref, buf, vat, guard)
# under the License. import weakref from typhon.atoms import getAtom from typhon.autohelp import autohelp from typhon.enum import makeEnum from typhon.errors import Refused, UserException, userError from typhon.log import log from typhon.objects.auditors import deepFrozenStamp, selfless from typhon.objects.constants import NullObject, unwrapBool, wrapBool from typhon.objects.data import StrObject from typhon.objects.root import Object, audited from typhon.vats import currentVat BROKEN, EVENTUAL, NEAR = makeEnum(u"RefState", u"broken eventual near".split()) BROKEN_1 = getAtom(u"broken", 1) FULFILLMENT_1 = getAtom(u"fulfillment", 1) ISBROKEN_1 = getAtom(u"isBroken", 1) ISDEEPFROZEN_1 = getAtom(u"isDeepFrozen", 1) ISEVENTUAL_1 = getAtom(u"isEventual", 1) ISFAR_1 = getAtom(u"isFar", 1) ISNEAR_1 = getAtom(u"isNear", 1) ISRESOLVED_1 = getAtom(u"isResolved", 1) ISSELFISH_1 = getAtom(u"isSelfish", 1) ISSELFLESS_1 = getAtom(u"isSelfless", 1) ISSETTLED_1 = getAtom(u"isSettled", 1) MAKEPROXY_3 = getAtom(u"makeProxy", 3) OPTPROBLEM_1 = getAtom(u"optProblem", 1) PROMISE_0 = getAtom(u"promise", 0)
from rpython.rlib.objectmodel import specialize from typhon.enum import makeEnum OK, ERR, LOOP_BREAK, LOOP_CONTINUE = makeEnum( "Future", ("OK", "ERR", "LOOP_BREAK", "LOOP_CONTINUE")) @specialize.argtype(0) def Ok(value): return (OK, value, None) @specialize.argtype(0) def Err(err): return (ERR, None, err) @specialize.argtype(0) def Break(value): return (LOOP_BREAK, value, None) def Continue(): return (LOOP_CONTINUE, None, None) class Future(object): pass class IOEvent(object): pass
# documented for the (hypothetical) E native code generator at # http://www.erights.org/enative/varcases.html . We associate each name with a # "slot", which is much like a standard Monte slot, but can be customized in # storage for the type of slot. Slot types are annotated onto each name, # providing information to the peephole optimizer and ensuring that frame # accesses cannot be type-confused. from typhon.enum import makeEnum # The different kinds of syntactic slots: # Final, implicit Any: def x # Final, guarded: def x :G # Var, implicit Any: var x # Var, guarded: var x :G # Binding: def &&x finalAny, final, varAny, var, binding = makeEnum( u"depth", u"finalAny final varAny var binding".split()) class SlotType(object): """ A type which describes the storage requirements of a slot. """ _immutable_ = True def __init__(self, depth, escapes): self.depth = depth self.escapes = escapes def repr(self): return u"<slotType %s%s>" % (self.depth.repr,
class FileDrain(Object): """ A drain for a file. """ fount = None pos = 0 # State machine: # * READY: Bufs are empty. # * WRITING: Bufs are partially full. Write is pending. # * BUSY: Bufs are overfull. Write is pending. # * CLOSING: Bufs are partially full. Write is pending. New writes cause # exceptions. # * CLOSED: Bufs are empty. New writes cause exceptions. READY, WRITING, BUSY, CLOSING, CLOSED = makeEnum( u"FileDrain", u"ready writing busy closing closed".split()) _state = READY def __init__(self, fs, fd, vat): self.fs = fs self.fd = fd self.vat = vat self.bufs = [] # Set this up only once. ruv.stashFS(self.fs, (self.vat, self)) def recv(self, atom, args): if atom is FLOWINGFROM_1: self.fount = args[0] return self if atom is RECEIVE_1: data = unwrapBytes(args[0]) self.receive(data) return NullObject if atom is FLOWSTOPPED_1: # Prepare to shut down. Switch to CLOSING to stop future data from # being queued. self.closing() return NullObject if atom is FLOWABORTED_1: # We'll shut down cleanly, but we're going to discard all the work # that we haven't yet written. self.bufs = [] self.closing() return NullObject raise Refused(self, atom, args) def receive(self, data): if self._state in (self.READY, self.WRITING, self.BUSY): self.bufs.append(data) if self._state is self.READY: # We're not writing right now, so queue a write. self.queueWrite() self._state = self.WRITING else: raise userError(u"Can't write to drain in state %s" % self._state.repr) def abort(self, reason): if self.fount is not None: with scopedVat(self.vat): from typhon.objects.collections.maps import EMPTY_MAP self.vat.sendOnly(self.fount, ABORTFLOW_0, [], EMPTY_MAP) self.closing() def queueWrite(self): with ruv.scopedBufs(self.bufs) as bufs: ruv.fsWrite(self.vat.uv_loop, self.fs, self.fd, bufs, len(self.bufs), self.pos, writeCB) def closing(self): if self._state is self.READY: # Optimization: proceed directly to CLOSED if there's no # outstanding writes. self.queueClose() self._state = self.CLOSING def queueClose(self): ruv.fsClose(self.vat.uv_loop, self.fs, self.fd, ruv.fsDiscard) self._state = self.CLOSED def written(self, size): self.pos += size bufs = [] for buf in self.bufs: if size >= len(buf): size -= len(buf) elif size == 0: bufs.append(buf) else: assert size >= 0 bufs.append(buf[size:]) size = 0 self.bufs = bufs if self.bufs: # More bufs remain to write. Queue them. self.queueWrite() # If we were CLOSING before, we're still CLOSING now. Otherwise, # we transition (from READY/WRITING) to WRITING. if self._state is not self.CLOSING: self._state = self.WRITING elif self._state is self.CLOSING: # Finally, we're out of things to do. Request a close. self.queueClose() else: # We are ready for more work. self._state = self.READY
from typhon.nano.slots import NoAssignIR """ Static scope analysis, in several passes: * Discovering the static scope layout * Removing meta.context() * Removing meta.state() * Laying out specialized frames * Deslotification """ def scopeError(layout, message): return userError(u"Error in scope of %s: %s" % (layout.fqn, message)) # The scope of a name; where the name is defined relative to each name use. SCOPE_OUTER, SCOPE_FRAME, SCOPE_LOCAL = makeEnum(u"scope", [u"outer", u"frame", u"local"]) # The severity of a name; how deeply-reified the binding and slot of a name # are in the actual backing storage of a frame. SEV_NOUN, SEV_SLOT, SEV_BINDING = makeEnum(u"severity", [u"noun", u"slot", u"binding"]) def layoutScopes(ast, outers, fqn, inRepl): """ Perform scope analysis. """ layoutPass = LayOutScopes(outers, fqn, inRepl) ast = layoutPass.visitExpr(ast) topLocalNames, localSize = layoutPass.top.collectTopLocals() return ast, layoutPass.top.outers, topLocalNames, localSize
# documented for the (hypothetical) E native code generator at # http://www.erights.org/enative/varcases.html . We associate each name with a # "slot", which is much like a standard Monte slot, but can be customized in # storage for the type of slot. Slot types are annotated onto each name, # providing information to the peephole optimizer and ensuring that frame # accesses cannot be type-confused. from typhon.enum import makeEnum # The different kinds of syntactic slots: # Final, implicit Any: def x # Final, guarded: def x :G # Var, implicit Any: var x # Var, guarded: var x :G # Binding: def &&x finalAny, final, varAny, var, binding = makeEnum(u"depth", u"finalAny final varAny var binding".split()) class SlotType(object): """ A type which describes the storage requirements of a slot. """ _immutable_ = True def __init__(self, depth, escapes): self.depth = depth self.escapes = escapes def repr(self): return u"<slotType %s%s>" % (self.depth.repr, u" (escapes)" if self.escapes else u"")
CALL, CALL_MAP, BUILD_MAP, NAMEDARG_EXTRACT, NAMEDARG_EXTRACT_OPTIONAL, JUMP, CHECKPOINT, ) = makeEnum("SmallCaps op", [ s.strip() for s in """ dup rot pop swap assignGlobal assignFrame assignLocal bind bindFinalSlot bindVarSlot bindAnyFinal bindAnyVar slotGlobal slotFrame slotLocal nounGlobal nounFrame nounLocal bindingGlobal bindingFrame bindingLocal listPatt literal bindObject scope ejector try unwind endHandler branch call callMap buildMap namedArgExtract namedArgExtractOptional jump checkpoint """.split() ]) ops = { "DUP": DUP, "ROT": ROT, "POP": POP, "SWAP": SWAP, "ASSIGN_GLOBAL": ASSIGN_GLOBAL,