https://github.com/shaina-gh/machine-code-generation
A C++ program that simulates compiler backend phases by converting infix expressions into postfix notation, quadruples, and machine-level code.
https://github.com/shaina-gh/machine-code-generation
compiler-design cpp infix-to-postfix machine-code-generation machine-level-code quadruples
Last synced: about 1 year ago
JSON representation
A C++ program that simulates compiler backend phases by converting infix expressions into postfix notation, quadruples, and machine-level code.
- Host: GitHub
- URL: https://github.com/shaina-gh/machine-code-generation
- Owner: shaina-gh
- Created: 2025-06-06T07:37:25.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-06-06T07:40:31.000Z (about 1 year ago)
- Last Synced: 2025-06-06T08:31:48.195Z (about 1 year ago)
- Topics: compiler-design, cpp, infix-to-postfix, machine-code-generation, machine-level-code, quadruples
- Language: C++
- Homepage:
- Size: 0 Bytes
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Machine Code Generation
---
## 🎯 Aim
To develop a compiler phase that converts a given infix arithmetic expression into:
1. Postfix notation (Reverse Polish Notation),
2. Quadruples (Intermediate Code Representation),
3. Three-Address / Machine Code.
---
## 🧠 Explanation
This program simulates the backend phase of a compiler, which is responsible for generating intermediate and target code from a high-level expression. It performs:
- **Conversion from infix to postfix** using operator precedence and a stack.
- **Generation of quadruples**, a standard intermediate representation in compilers using temporary variables.
- **Creation of Three-Address Code (TAC)** or **Machine code-like instructions** for arithmetic operations.
This process mirrors how a real compiler breaks down high-level arithmetic into machine-executable code.
---
## 🔁 Logic
### Step-by-step Process:
1. **Infix to Postfix Conversion**:
- Use a stack to manage operators and apply precedence rules to form the postfix expression.
2. **Quadruple Generation**:
- Traverse the postfix expression.
- For each operator, pop the top two operands, apply the operation, and store the result in a temporary variable `t1, t2...`.
3. **Three-Address Code (TAC) / Machine Code Generation**:
- For each operation, generate equivalent low-level instructions:
- Use `MOV`, `ADD`, `MUL`, and `POW` instructions for computation.
- Results are stored in temporaries and finally assigned to a variable `a`.
---
## 📥 Sample Input & Output
### ✅ Input
```cpp
Input an expression: a+b*c
```
### 🔁 Output
```cpp
Postfix Expression: abc*+
Quadruples:
(+, a, t1, t2)
(*, b, c, t1)
Three-Address Code/Machine Code:
MOV b, R0
MUL c, R0
MOV R0, t1
MOV a, R0
ADD t1, R0
MOV R0, t2
MOV t2, a
```
---
## 🌐 Real-World Applications
- **Compiler Construction**: Forms the core of backend phases in compilers like GCC or Clang.
- **Virtual Machines**: Used in interpreters like Python (CPython) or JVM to generate bytecode.
- **Code Optimisation Tools**: Intermediate representations, such as quadruples, are used for optimisation passes.
- **Static Analysis Tools**: These tools utilise such representations to detect bugs, inefficiencies, or vulnerabilities.
---