How to run compiler to convert source code to linear IR:
- Need python3
- Clone the repository and navigate to it
- Make a valid cpp file that follows grammar below and utilizes valid operators
- Run by entering in the command line python3 main.py ./(enter path to the cpp file you created)
- Resulting intermediate representation will be outputted
Valid Operators:
- Arithmetic operators: +, -, *, /
- Comparison operators: ==, <
- Assignment operator: =
- Braces/parentheses: {, }, (, )
- Semicolon: ;
- ID: letter followed by series of letters/numbers
- Number: integer or floating point value
Expected program grammar:
Function header
- Starts with void, int or float
- Followed by sequence of references (sequence can be empty) to either int or float variables enclosed in parentheses. Ex: (int &x, float &y)
- Followed by sequence of statements enclosed in braces. Look through the test cpp files for reference
Declaration statement
- Only int and float data types are allowed
- ID must start with a letter and can be followed by any sequence of letters and numbers
Ex: int x; or float y;
Assignment statement
- LHS must be a variable that has already been declared
- RHS must be a valid expression followed by a semicolon
ex: x = 3+4-(4*9);
If-else statement
- “if” followed by a set of parentheses that encloses a valid expression followed by “else” followed by a valid statement
ex: if(x>5) x = 2; else x = 3
Block statement
- { followed by a sequence of statements followed by }
ex: {x=5; y=2;}
For loop statement
- “for” followed by left parenthesis followed by a valid assignment statement with a semicolon followed by a valid expression followed by a semicolon followed by a valid assignment statement without a semicolon followed by a right parenthesis followed by a valid statement
ex: for(x = 5; x<20; x = x+1){ y = 27; } *note: a block statement is considered a valid statement
Error Reporting
- If program is invalid at a specific point (ex: missing semicolon), the parser will raise an exception and let you know the exact line number, what it expected, and what it received instead
- Use standard scoping rules for c++. Any errors in scoping will trigger a SymbolTable exception
Linear Intermediate Representation
- Unlimited virtual registers
- Virtual registers can be assigned to each other
- virtual register operations: add, sub, mult, div, lt, eq
- Each register operation takes in two virtual registers. Each operation is followed by either i or f. i - both registers must be ints f - both registers must be floats Ex: addi(vr0, vr7);
- Branch: branch, beq, bne
- Labels = label name followed by colon