aalhour / pycoolc Goto Github PK
View Code? Open in Web Editor NEW:snake: :cool: Compiler for the COOL programming language in Python 3
License: MIT License
:snake: :cool: Compiler for the COOL programming language in Python 3
License: MIT License
Three main types in Compiler Research:
Algebraic Expressions Simplification:
x = x + 0 => x = x
x = x * 1 => x = x
x = x * 0 => x = 0
x = x + 8 => x = x << 3
x = x ** x => x = x * x
Constant Folding:
a = 2 * 3 => a = 6
b = 1 + 2 => b = 3
Dead Code Elimination. Dead code is code that does't contribute to the program's result.
Peephole Optimisations.
SSA-based Optimisations:
a = x * x
...
d = x * x => d = a
Copy Propagation:
b = x => gets eliminated and all references changed to "x"
c = b * 2 => c = x * 2
Constant Propagation:
a = 2 => *eliminated*
b = a + 2 => b = 2 + 2 => b = 4
c = b * 3 => c = 4 * 3 => c = 12
I looked at page 15 of the Cool Manual and it says that '\v' is a whitespace character. I used your lexer to check my own custom lexer that I wrote in Haskell and it looks like \v is not treated as a whitespace character. Can this be fixed?
TODO...
TODO...
Cool Identifier Bindings are introduced by:
Object
type.Object
type.Int
, Bool
, String
and SELF_TYPE
.Object
is at the root and all other types branching down from it, this is also called the inheritance tree
.lub
) relation of two types is their least common ancestor in the inheritance tree.C
can also be used on type C'
, where C'
<= C
, meaning C'
is a subtype of C
.foo
and an object foo
can coexist in the same scope.O
: a function providing mapping from types to Object Identifiers and vice versa.M
: a function providing mapping from types to Method Names and vice versa.SELF_TYPE
, we need to know the class name at all points of Type Checking methods.
C
: a function providing the name of the current class (Type).SELF_TYPE
is not a Dynamic Type, it is a Static Type.
SELF_TYPE
is the type of the self
parameter in an instance. In a method dispatch, SELF_TYPE
might be a subtype of the class in which the subject method appears.
SELF_TYPE
can be used with new T
expressions.SELF_TYPE
can be used as the return type of class methods.SELF_TYPE
can be used as the type of expressions (i.e. let expressions: let x : T in expr
).SELF_TYPE
can be used as the type of the actual arguments in a method dispatch.SELF_TYPE
can not be used as the type of class attributes.SELF_TYPE
can not be used with Static Dispatch (i.e. T
in m@T(expr1,...,exprN)
).SELF_TYPE
can not be used as the type of Formal Parameters.lub(SELF_TYPE.c, SELF_TYPE.c) = SELF_TYPE.c
.lub(SELF_TYPE.c, T) = lub(C, T)
.lub(T, SELF_TYPE.c) = lub(C, T)
.[incomplete]
Two solutions:
Object
to ill-typed expressions.No_Type
for use with ill-typed expressions.Solution 1 is easy to implement and will enforce the type inheritance and class hierarchy tree structures.
Solution 2 will introduce further adjustments. First, every operation will be treated as defined for No_Type
. Second, the inheritance tree and class hierarchy will change from being Trees to Graphs. The reason for that is that expressions will ultimately either be of type Object
or No_Type
, which will make the whole representation look like a graph with two roots.
Parsing the /examples/graph.cl
program leads to shift/reduce conflicts in the parser.
Parser output sample:
Generating LALR tables
WARNING: 9 shift/reduce conflicts
Assumes information exists on all points of the program execution, which is typically gathered through Data Flow Analysis.
Data Flow Analysis runs in terms of relating information between adjacent program points by either transferring information or pushing information. Information about expressions gets transferred from the output of predecessor expressions to the input of their proceeding ones. Pushing information runs in terms of the body of each and every expression from the input of that expression until its output. Transferring information is an analysis of information running from one expression to the other, as adjacent points in the program, whereas pushing information is analysis of the flow of information within the scope of every expression.
Typically Data Flow Analysis tags all SSA-expressions in the execution of the program with 3 tags: C
for constant, T
for top and B
for bottom. Constant means that the value of the expression up until the given point of execution is a constant and therefore can be propagated with a constant. Top means that up until the given point of execution we don't know the value of the expression. Bottom means that up until the given point of execution, the subject expression never excuses and therefore can be eliminated.
The ordering of these tags is as follows: B
< C
< T
.
The Least-Upper Bound logical operation calculates the upper bound in the previous ordering. For example:
B
, 1) = 1, where 1 and 2 are constants, and therefore tagged with C
.B
, C
) = C
.T
. Constants are incomparable.T
, B
) = T
.Given an ordered set of predecessor statements (SSA-expressions) called ps
, and a successor statement we are analysing called s
then we can define the following Constant Propagation rules.
We begin by defining the rule that relates information between adjacent points in the program:
s
, x
, in
) = lub { C(p
, x
, out
) | p
is a predecessor of s
}; where:
in
is the input to point of s
.
out
is the output point of every predecessor p
the belongs to the set ps
.
x
is an assignment statement target propagated until the execution point of s
, for example:
x := 1
y := x * 2
...
s := ...
This rule relates the out
of one statement to the in
of the next statement.
Next, we define the following rules which relate the in
of a statement to the out
of the same statement:
s
, x
, out
) = B
if C(s
, x
, in
) = B
.x := c
, x
, out
) = c if c is a constant (C
).x := f(...)
, x
, out
) = T
. We don't evaluate the complex inner statement.y := ...
, x
, out
) = C(y := ...
, x
, in
) if x
<> y
; where:
y := ...
means that it's an statement that does't read nor update x
.For every entry s
to the program, set C(s
, x
, in
) = T
.
Set C(s
, x
, in
) = C(s
, x
, out
) = B
everywhere else.
Repeat until all points satisfy the above rules (1-5):
3.1. Pick s
, where s
is not satisfying rules 1-5 and update it using the appropriate rule.
Global Constant Propagation is a forward-probagation analysis. This analysis runs from earlier points in the program to later ones.
Also know as Live Variables Analysis.
A variable x
is live at statement s
if:
s'
that uses x
.s
to s'
.x
.A statement x := ...
is dead code if x
is dead after the assignment.
We can express Livelinessin terms of information transferred between adjacent statements, just as in copy/constant propagation. Except that it is much simpler. Liveliness is a boolean: True
or False
. A statement is either live or not.
Given a predecessor statement p
and a set of successor statements ss
, we can define the following Liveliness Analysis rules:
p
, x
, out
) = v { L(s
, x
, in
) | s
a successor of p
}; where v is the logical OR operator.s
, x
, in
) = True
if s
refers to x
on the rhs.
Example:
... => x is live!
... := f(x)
... => x is live!
x := e
, x
, in
) = False
if e
does not refer to x
.s
, x
, in
) = L(s
, x
, out
) if s
does not refer to x
.Let all L(...) = False
initially.
Repeat until all statements s
satisfy rules 1-4:
2.1. Pick s where one of 1-4 does not hold and update using the appropriate rule
Global Liveness Analysis is a backward-probagation analysis. This analysis runs backwards from later points in the program to earlier ones.
markdown
document.Bug. The compiler pycoolc
doesn't generate outfile when using the --outfile
option.
Description. I installed pycoolc
on my Linux Ubuntu system, and it installed correctly. Then, I tried to compile a simple "hello world" program in COOL using pycoolc
, and it compiled. But when I used pycoolc to generate an outfile so that I could run it using spim
, it didn't generate any.
Terminal interaction. This is exactly what happened when I tried using your compiler on my hello.cl
file:
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# # Finding all files staring with 'h'
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# find h*
hello.cl
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# # hello.cl exists; now compiling using pycoolc
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# pycoolc hello.cl
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# # compiled; now generating outfile
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# pycoolc hello.cl --outfile helloAsm.s
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# # Let's see if this file exists
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# find h*
hello.cl
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# find helloAsm.s
find: ‘helloAsm.s’: No such file or directory
root@rafier:/media/rafi007akhtar/Stuff/UEM/Sem 7 Stuff/COOL# # Clearly, compiler doesn't generate outfile
In case you're interested, here is source code of hello.cl
.
Looking forward to seeing the issue resolved!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.