优化代码
This commit is contained in:
parent
78c18419ef
commit
d6cfa02e79
@ -14,4 +14,19 @@
|
|||||||
<option name="Make" enabled="true" />
|
<option name="Make" enabled="true" />
|
||||||
</method>
|
</method>
|
||||||
</configuration>
|
</configuration>
|
||||||
|
<configuration default="false" name="SnowCompiler" type="Application" factoryName="Application" activateToolWindowBeforeRun="false" nameIsGenerated="true">
|
||||||
|
<option name="ALTERNATIVE_JRE_PATH" value="23" />
|
||||||
|
<option name="MAIN_CLASS_NAME" value="org.jcnc.snow.compiler.cli.SnowCompiler" />
|
||||||
|
<module name="SCompiler" />
|
||||||
|
<option name="PROGRAM_PARAMETERS" value="test" />
|
||||||
|
<extension name="coverage">
|
||||||
|
<pattern>
|
||||||
|
<option name="PATTERN" value="org.jcnc.snow.compiler.parser.preprocessor.lexer.impl.api.*" />
|
||||||
|
<option name="ENABLED" value="true" />
|
||||||
|
</pattern>
|
||||||
|
</extension>
|
||||||
|
<method v="2">
|
||||||
|
<option name="Make" enabled="true" />
|
||||||
|
</method>
|
||||||
|
</configuration>
|
||||||
</component>
|
</component>
|
||||||
@ -3,4 +3,8 @@
|
|||||||
<envs />
|
<envs />
|
||||||
<method v="2" />
|
<method v="2" />
|
||||||
</configuration>
|
</configuration>
|
||||||
|
<configuration default="false" name="build_project2tar.ps1" type="PowerShellRunType" factoryName="PowerShell" scriptUrl="$PROJECT_DIR$/build/build_project2tar.ps1" executablePath="C:/WINDOWS/System32/WindowsPowerShell/v1.0/powershell.exe">
|
||||||
|
<envs />
|
||||||
|
<method v="2" />
|
||||||
|
</configuration>
|
||||||
</component>
|
</component>
|
||||||
@ -6,10 +6,9 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An intermediate-representation function (IRFunction).
|
* An intermediate‑representation function (IRFunction).
|
||||||
*
|
*
|
||||||
* <p>For every source-level function we build one IRFunction that
|
* <p>For every source‑level function we build one IRFunction that records:</p>
|
||||||
* records:</p>
|
|
||||||
* <ul>
|
* <ul>
|
||||||
* <li>the function’s name,</li>
|
* <li>the function’s name,</li>
|
||||||
* <li>a list of IR instructions (the <em>body</em>),</li>
|
* <li>a list of IR instructions (the <em>body</em>),</li>
|
||||||
@ -27,12 +26,12 @@ public class IRFunction {
|
|||||||
/** linear list of IR instructions */
|
/** linear list of IR instructions */
|
||||||
private final List<IRInstruction> body = new ArrayList<>();
|
private final List<IRInstruction> body = new ArrayList<>();
|
||||||
|
|
||||||
/* ---------- virtual-register management ---------- */
|
/* ---------- virtual‑register management ---------- */
|
||||||
|
|
||||||
/** running counter for new virtual registers */
|
/** running counter for new virtual registers */
|
||||||
private int regCounter = 0;
|
private int regCounter = 0;
|
||||||
|
|
||||||
/** list of formal-parameter registers in declaration order */
|
/** list of formal‑parameter registers in declaration order */
|
||||||
private final List<IRVirtualRegister> parameters = new ArrayList<>();
|
private final List<IRVirtualRegister> parameters = new ArrayList<>();
|
||||||
|
|
||||||
/* ---------- construction ---------- */
|
/* ---------- construction ---------- */
|
||||||
@ -43,7 +42,7 @@ public class IRFunction {
|
|||||||
|
|
||||||
/* ---------- register helpers ---------- */
|
/* ---------- register helpers ---------- */
|
||||||
|
|
||||||
/** Allocates and returns a brand-new virtual register. */
|
/** Allocates and returns a brand‑new virtual register. */
|
||||||
public IRVirtualRegister newRegister() {
|
public IRVirtualRegister newRegister() {
|
||||||
return new IRVirtualRegister(regCounter++);
|
return new IRVirtualRegister(regCounter++);
|
||||||
}
|
}
|
||||||
@ -51,7 +50,7 @@ public class IRFunction {
|
|||||||
/* ---------- parameter helpers ---------- */
|
/* ---------- parameter helpers ---------- */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adds a virtual register to the formal-parameter list.
|
* Adds a virtual register to the formal‑parameter list.
|
||||||
* Call this once for each parameter <em>in declaration order</em>
|
* Call this once for each parameter <em>in declaration order</em>
|
||||||
* when building the IR.
|
* when building the IR.
|
||||||
*/
|
*/
|
||||||
@ -79,9 +78,21 @@ public class IRFunction {
|
|||||||
|
|
||||||
/* ---------- debugging ---------- */
|
/* ---------- debugging ---------- */
|
||||||
|
|
||||||
@Override public String toString() {
|
@Override
|
||||||
StringBuilder sb = new StringBuilder("func " + name + " {\n");
|
public String toString() {
|
||||||
|
// Print function header with explicit parameter list: func name(%0, %1) {
|
||||||
|
StringBuilder sb = new StringBuilder("func ")
|
||||||
|
.append(name)
|
||||||
|
.append('(');
|
||||||
|
for (int i = 0; i < parameters.size(); i++) {
|
||||||
|
sb.append(parameters.get(i));
|
||||||
|
if (i < parameters.size() - 1) sb.append(", ");
|
||||||
|
}
|
||||||
|
sb.append(") {\n");
|
||||||
|
|
||||||
|
// Body instructions indented by two spaces
|
||||||
body.forEach(i -> sb.append(" ").append(i).append('\n'));
|
body.forEach(i -> sb.append(" ").append(i).append('\n'));
|
||||||
|
|
||||||
return sb.append('}').toString();
|
return sb.append('}').toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
14
test
14
test
@ -1,14 +1,12 @@
|
|||||||
module: CommonTasks
|
module: CommonTasks
|
||||||
function: main1
|
function: main
|
||||||
parameter:
|
parameter:
|
||||||
declare num1: int
|
|
||||||
declare num2: int
|
|
||||||
return_type:int
|
return_type:int
|
||||||
|
|
||||||
body:
|
body:
|
||||||
num1 = 1
|
|
||||||
num2=2
|
return CommonTasks.test(3,1)
|
||||||
return num1 + CommonTasks.test(3,4+1)
|
|
||||||
end body
|
end body
|
||||||
end function
|
end function
|
||||||
|
|
||||||
@ -19,8 +17,8 @@ module: CommonTasks
|
|||||||
return_type:int
|
return_type:int
|
||||||
body:
|
body:
|
||||||
declare result : int
|
declare result : int
|
||||||
declare num3: int =4
|
|
||||||
result = num1 + num2 +num3
|
result = num1 + num2
|
||||||
return result
|
return result
|
||||||
end body
|
end body
|
||||||
end function
|
end function
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user