1

Java with Generators Tony Young, Student ID: 5383914, University of Auckland

Abstract—This report details the implementation of generators in Java – a specialized case of continuations that allow the execution of code to be paused within a method body at specific yield points. It introduces linearization as the primary technique for achieving this, with additional supplementary algorithms to transform code to adhere to the semantics of Java.

I. I NTRODUCTION Generators are a language feature found in various modern languages. They enable sequential code to be paused at specific yield points, making it useful for tasks such as iterating through a binary tree or creating lazy, infinite lists (`a la Haskell) without having to explicitly keep track of where execution is. This implementation of generators extends Java syntax in two ways: • An extension to the Java method syntax. Before the method name, we allow an optional * token that signifies the method is a generator that will yield values of the type specified for the method. • The introduction of a yield statement. It is allowed only in starred generator methods and takes an expression that will be returned a subsequent execution of the generator. For simplicity’s sake, generators in this implementation are unidirectional and only yield values out (such as in C#), unlike the generators in Python or ECMAScript Harmony can have values “sent” to them.

B. Inheritance Resolution After scope collection, we do not have any information of inheritance relationships. As inheritance is similar to nested scopes, we just add the scopes of any superclasses to a subclass’s scope. In order to distinguish between subclasses and inner classes, we insert a phantom scope, the inner scope, between a class and an inner class. As such, we can climb class scopes to resolve inherited variables and stop once we reach a non-class scope. C. Type Analysis Type analysis uses an Hindley–Milner-style type inference engine – we apply assumptions about types and check if they match the conclusions supplied in the code. We can infer expected types from known types – for instance, we can infer the type of the right-hand side of an assignment by evaluating its type. As Java requires declaration of the type somewhere in the code, we will already have an expected conclusion for the type of the variable – which we simply check for equality against. For the purposes of inference, we can regard various expressions as type-level homomorphisms exist, such as field access and method calls. Additionally, we can regard field and variable declarations as conclusions for the purposes of Hindley–Milner. As such, it is relatively straightforward to apply these morphisms and eventually assert the well-typedness of a given variable.

II. S EMANTIC A NALYSIS As part of the assignment, some semantic analysis is implemented. The symbol table is generalized to encompass all kinds of scope – static, instance, class and block. Static scope represents either a static initializer block or a static method – one that only has access to static class methods. Instance scope represents instance methods that operate on instance variables, rather than static variables. Block scopes represent blocks in methods – such as those held by if, while etc. statements. The semantic analyzer performs three passes.

D. Further Work Unimplemented aspects include return type analysis (yeah, really), subclass covariance, static members, operators, generics, string literals and a lot more. Semantic analysis was not the focus of the project and, as such, is set to only generate warnings on failure. Errors can be forced using the -Wsemantic-error command line argument, or completely disabled with -fno-semantic. III. T RANSFORMATION

A. Scope Collection The somewhat badly named scope collection pass does two things – it collects all the types in scope and stores them in the environment. For each type, it constructs a new scope that contains all the fields and their types. At the same time, scope collection enters blocks to construct new scopes that contain all the variables in that given block’s scope. During scope collection, we can check if a block-level variable has inappropriately shadowed another block-level variable, or if a class-level method has inappropriately overridden another class-level method.

The code is transformed in a multi-pass fashion – the bulk of transformation is done during the linearization process (section III-C), but some pre- and post-processing passes are employed to make linearization simpler. A. Loop Desugaring The first phase of generator transformation is loop desugaring. In this phase, we transform all loops into a canonical form, which is proposed to be the following: for (;;) { /* loop body */ }

2

The canonical loop form represents an infinite loop – however, the loop body may contain break and continue statements to mimic sugared loop forms. As such, automated conversion can be employed from other forms of loops (such as while, for and do). For a full list of conversions from canonical loop forms, refer to appendix A. In the special case of a non-canonical for loop, we may need to move an outer label into the inner canonical for loop. This is facilitated by remembering the position of labels before entering their statement nodes as well as which statement node was entered then, during processing of the statement node, we can indicate that we have moved the label into a loop and that the label needs to be removed. B. Scope Mangling Java employs block-level scoping for variables – for example, variables declared inside an if block will not be visible upon exit of the if block. Due to linearization (section III-C), we cannot preserve blocks and, consequently, we cannot use variable names in the form they are suggested by the code, for example: { int i = 0; } { float i = 0.0f; } i is typed as an int in the first scope, but float in the second. If the statements are placed into a single block from both the blocks, we will find that we have a variable name clash, as well as the variable i having its type changed. As such, we mangle variable names in deeper scopes to ensure they don’t conflict with variable names in higher scopes. Figure 1 details a basic algorithm for name mangling. We also perform partial semantic analysis of variable declaration at this point, ensuring that local variables are not redeclared. We maintain a stack of block scopes that represents the current block of code we’re in, as well as all the ancestral blocks. The block scopes contain symbol tables that keep track of variable declarations in this scope – if we find that a variable has been redeclared either in this scope or any ancestral scope, this is a semantic error. In practice, the block ID is not a simple monotonically increasing number. For the implementation provided, the blocks are first numbered off by their offset to the start of the parent. The variables are then mangled using successive prepending of block numbers from the block the variable is declared in to the root of the method – for example, in the example given earlier, the three variables will be mangled to s0$i, s1$i. If a variable j was declared inside the second block inside a block, then it will be mangled to s1$s0$j. C. Linearization The linearization (pertaining to loop linearization) pass processes the control flow graph and transforms it into a state machine. This is required because we can then create states

function S COPE M ANGLE(node) id ← 0 for all b ∈ node’s blocks do for all d ∈ b’s declarations do P REPEND(id, d’s variable name) end for for all e ∈ b’s expressions do for all n ∈ e’s referenced names do P REPEND(n’s block’s id, n) end for end for id ← id + 1 end for end function Figure 1.

Scope mangling algorithm.

function M ARK L INEARIZATION(node) if node ∈ yields ∪ breaks ∪ continues ∪ returns then M ARK(node) return True end if for all n ∈ node’s children do if M ARK L INEARIZATION(n) then M ARK(n) end if end for if nodes were marked then return True else return False end if end function Figure 2.

Linearization marking algorithm.

that represents the pausing of the state machine, at given yield points. Additionally, linearized control structures enable us to perform arbitrary jumps into the code – such as jumps into code after the yield point to simulate resumes. Before linearization, we employ a node marking scheme to avoid excessive linearization. The algorithm is detailed in figure 2 details which nodes are selected to be linearized. In particular, any node containing a yield, break or continue must be linearized, as well as all of their ancestors. Once we have the nodes for linearization, we apply the straightforward linearization transforms to control flow as described in appendix B. States are in the form of case statements, which denote a state number that can be jumped to by setting the appropriate state number and breaking from the case. For yield statements, we create a state for resuming execution when we resume the state machine. In the previous state, we create a deferred jump to the resume state, set the yielded value as the current value emitted by the state machine and then return true from the state machine to signify that the current value has changed. We keep track of two things during linearization: labels and

3

loops. A loop can be considered an implicit label that is given a name using the enumeration order. A label refers to a statement that can be jumped to, but only from a child statement of the labeled statement. With loop and label state, the following conditions apply to the linearization transformer: •



When the transformer encounters a label statement, we push the label onto the stack of labels with its name, as well as onto the map of labels indexed by string. The label will also have the start state associated with it, i.e. the point we will jump to if we continue to it. After we process the label’s child statement, we know the state at which the statement ends so we set that as the point that we jump to if we attempt to break to it. During transformation of child break and continue statements, we verify that we only ever attempt to break or continue to labels found in parent node. If a label is directly adjacent to a loop, the label is marked as being a loop label (and consequently allows continues). When the transformer encounters a canonical loop or a switch statement, we repeat the same procedure we did with the labels. The transformer will then transform any label-less break and continue statements it finds into labeled ones, using special labels prefixed with .loop or .switch for loops and switches, respectively, followed by the block number as dictated by enumeration order. We only allow continue statements to jump to an ancestral loop block.

After all the code has been linearized, we create a trap state that always deferred-jumps to itself and returns false. This ensures we don’t execute random code in the state machine. 1) Labeled Jump Resolution: The initial linearization pass should have already verified that we aren’t performing jumps to non-ancestral blocks. As such, we just look up labels in the linearization context and generate jumps to them for break statements. We do the same for continue statements, except we use the continue points instead of the break points. At this point, we expect no more breaks or continues that haven’t been dereferenced. D. Wrapping Linearization will have created a full state machine, which must then be wrapped in a switch statement wrapped in an infinite loop to facilitate jumping between states. This implementation supplies a runtime class, genja.rt.Generator, that wraps the generator to conform to Java’s iterator interface. This class adopts slightly different semantics from standard iterators, due to the fact that generators are slightly different to Java iterators: •



A hasNext() call on a genja.rt.Generator will resume the underlying state machine and set the last state machine success result if and only if the generator’s stale flag is set to true. If the stale flag is not set, we simply return the last state machine success result. A next() call on a genja.rt.Generator will make a call to hasNext() if and only if the generator’s stale flag is set to true. Otherwise, we return the current

value yielded from the state machine and set the stale flag to true. IV. C ONCLUSION Through a multi-pass compilation process, we can effectively generate state machines in Java via linearization of control flow. These can then be in turn transformed into generators and made compatible with Java’s iterators to facilitate various interesting constructs, such as infinite lazy lists and abandonable loop processing from yield points. A PPENDIX A. Canonical Loop Forms Here are the canonical loop forms for various types of loops. 1) while: label: while (pred) { body; } becomes: label: for (;;) { if (!pred) break; body; } 2) do: label: do { body; } while (pred) becomes: label: for (;;) { body; if (!pred) break; } 3) for (non-canonical loop): label: for (init; pred; update) { body; } becomes: { init; label: for (;;) { if (!pred) break; body; update; } } B. Linearizations Here are linearized forms for various control structures, as well as yield. The case numbers only intend to denote flow. 1) if: preamble; if (pred) { consequent; } else { alternate; } postamble;

4

becomes: case 0: preamble; if (pred) { state = 1; break; } else { state = 2; break; } case 1: consequent; state = 3; break; case 2: alternate; case 3: postamble; 2) for (canonical loop): preamble; for (;;) { body; } postamble; becomes: case 0: preamble; case 1: body; case 2: state = 1; break; case 3: postamble; 3) switch (with default): preamble; switch (x) { case A: bodyA; break; case B: bodyB; case C: case D: bodyD; break; default: bodyDefault; } postamble; becomes: case 0: preamble;

switch (x) { case A: state = 1; break; case B: state = 2; break; case C: state = 3; case D: state = 4; break; default: state = 5; break; } break; case 1: bodyA; state = 6; break; case 2: bodyB; case 3: state = 4; case 4: bodyD; state = 6; break; case 5: bodyDefault; case 6: postamble; 4) switch (without default): preamble; switch (x) { case A: bodyA; break; case B: bodyB; case C: case D: bodyD; break; } postamble; becomes: case 0: preamble; switch (x) { case A: state = 1; break; case B:

5

state = break; case C: state = break; case D: state = break; default: state = break; } break; case 1: bodyA; state = 5; break; case 2: bodyB; case 3: state = 4; case 4: bodyD; state = 5; break; case 5: postamble; 5) yield: preamble; yield x; postamble; becomes: case 0: preamble; case 1: state = 2; current = x; return true; case 2: postamble;

2;

3;

4;

5;

Java with Generators - GitHub

processes the control flow graph and transforms it into a state machine. This is required because we can then create states function SCOPEMANGLE(node).

50KB Sizes 2 Downloads 311 Views

Recommend Documents

Advance-Java-Suresh - GitHub
Page 1. ameerpetmaterials.blogspot.in. For More Tutorials Visit. Page 2. Advance Java. (Suresh Sir). Page 3. Page 4. Page 5. Page 6. Page 7. Page 8. Page 9. Page 10 .... Page 141. Page 142. Page 143. Page 144. Page 145. Page 146. Page 147. Page 148.

Java 7 - GitHub
Sep 8, 2011 - Memory sizing options. Alexey Ragozin – http://blog.ragozin.info. Available combinations of garbage collection algorithms in HotSpot JVM.

FRC Java Programming - GitHub
FRC Java Programming Last Updated: 1/11/2016 ..... NI Update Service .... network. When you have entered the team number and the roboRIO is connected, ...

QR Reader Java Project - GitHub
QR Reader Java Project. Date: 4 July ... Open eclipse and click on File -> import -> General -> Existing projects into workspace as shown in fig. 2. ... Similarly fix problem of JRE system library if in ... ProjectFlow.pdf show complete flow of proje

Senior Java Developer - Technical Exercise - GitHub
Stretch goals – to be implemented only if you have time. 3. The program should expose 2 rest endpoints returning: * OK if the application is running. * Version of the application. 4. Please submit your source code along with: •. A README document

of dc generators -
b) Explain the regulation of an alternator by emf method. 16. a) Explain V curves as applied to synchronous motors. b) Describe about stepper motorand also its ...

More Efficient DDH Pseudorandom Generators
University of Calgary, Calgary, Canada T2N 1N4 ... review the literature of number-theoretic PRGs, and refer the reader to [4, 8, 18, 19, 37] for ..... needs 8 exponentiations with n-bit exponent by the square-multiply method (See Appendix A for.

CP2K with LIBXSMM - GitHub
make ARCH=Linux-x86-64-intel VERSION=psmp AVX=2. To target for instance “Knights ... //manual.cp2k.org/trunk/CP2K_INPUT/GLOBAL/DBCSR.html).

Misleading Worm Signature Generators Using ... - Roberto Perdisci
this case the real worm flow and all its fake anomalous flows ... gorithm creates an ordered list of tokens that is present in all the .... Of course, the question is how to obtain p(false ...... Proceedings of the 10th ACM Conference on Computer and

Minecraft Generators Mod 418
Sign in; Search settings; Web History : Advanced search Language tools: Advertising ... channel laptop, PC, Mobile, Desktop, Computer etc. so enjoy it Code ... Hack Programming Video Games vs Free Game Generator Codes Minecraft ...

Misleading Worm Signature Generators Using Deliberate ... - CiteSeerX
tain TI. Whenever two worm flows wi and wj are consid- ered together, a signature containing TI will be generated. Whereas, whenever two fake anomalous ...

Misleading Worm Signature Generators Using ... - Roberto Perdisci
jected code, e.g., [15, 10, 5, 1] are not considered because they are largely ... to a host in B, it also sends a fake anomalous flow to the same host, as shown in ...

OpenBMS connection with CAN - GitHub
Arduino with BMS- and CAN-bus shield as BMS a master. - LTC6802-2 or LTC6803-2 based boards as cell-level boards. - CAN controlled Eltek Valere as a ...

Better performance with WebWorkers - GitHub
Chrome52 on this Laptop. » ~14kbyte. String => 133ms ... 3-4 Seks processing time on samsung galaxy S5 with crosswalk to finish the transition with ... Page 17 ...

with ZeroMQ and gevent - GitHub
Normally, the networking of distributed systems is ... Service Oriented .... while True: msg = socket.recv() print "Received", msg socket.send(msg). 1. 2. 3. 4. 5. 6. 7.

Getting Started with CodeXL - GitHub
10. Source Code View . ..... APU, a recent version of Radeon Software, and the OpenCL APP SDK. This document describes ...... lel_Processing_OpenCL_Programming_Guide-rev-2.7.pdf. For GPU ... trademarks of their respective companies.

Getting Started with Go - GitHub
Jul 23, 2015 - The majority of my experience is in PHP. I ventured into Ruby, ... Compiled, Statically Typed, Concurrent, Imperative language. Originally ...

Getting Acquainted with R - GitHub
In this case help.search(log) returns all the functions with the string 'log' in them. ... R environment your 'working directory' (i.e. the directory on your computer's file ... Later in the course we'll discuss some ways of implementing sanity check

Training ConvNets with Torch - GitHub
Jan 17, 2014 - ... features + SVM. – Neural Nets (and discuss discovering graph structure automařcally). – ConvNets. • Notebook Setup ... Page 9 ...

Examples with importance weights - GitHub
Page 3 ... Learning with importance weights y. wT t x wT t+1x s(h)||x||2 ... ∣p=(wt−s(h)x)Tx s (h) = η. ∂l(p,y). ∂p. ∣. ∣. ∣. ∣p=(wt−s(h)x)Tx. Finally s(0) = 0 ...

Digital Design with Chisel - GitHub
Dec 27, 2017 - This lecture notes (to become a book) are an introduction into hardware design with the focus on using the hardware construction language Chisel. The approach of this book is to present small to medium sized typical hardware components

Deep Learning with H2O.pdf - GitHub
best-in-class algorithms such as Random Forest, Gradient Boosting and Deep Learning at scale. .... elegant web interface or fully scriptable R API from H2O CRAN package. · grid search for .... takes to cut the learning rate in half (e.g., 10−6 mea

Small Thermoelectric Generators - The Electrochemical Society
with small heat sources and small ... temperature gradient.2 As the heat flows .... 2. Conceptual design of thermoelectric generator producing electricity from.