<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Optimization | Guillermo F. Parra</title><link>https://guillermo.parralopez.com/tags/optimization/</link><atom:link href="https://guillermo.parralopez.com/tags/optimization/index.xml" rel="self" type="application/rss+xml"/><description>Optimization</description><generator>HugoBlox Kit (https://hugoblox.com)</generator><language>en-us</language><lastBuildDate>Sat, 09 May 2026 00:00:00 +0000</lastBuildDate><image><url>https://guillermo.parralopez.com/media/icon_hu_c22ca16efde0ca17.png</url><title>Optimization</title><link>https://guillermo.parralopez.com/tags/optimization/</link></image><item><title>miniC to MIPS Compiler</title><link>https://guillermo.parralopez.com/projects/compiler/</link><pubDate>Sat, 09 May 2026 00:00:00 +0000</pubDate><guid>https://guillermo.parralopez.com/projects/compiler/</guid><description>&lt;h3 id="description"&gt;Description&lt;/h3&gt;
&lt;p&gt;A complete compiler from &lt;strong&gt;miniC&lt;/strong&gt;, a small C-like language, to &lt;strong&gt;MIPS assembly&lt;/strong&gt;, built for the Compilers course at the University of Murcia (2025/2026) in a team of two.&lt;/p&gt;
&lt;p&gt;The compiler covers the four classic phases: lexical analysis (Flex), parsing (Bison), semantic analysis over a symbol table, and MIPS code generation. The generated assembly was validated by running it in the MARS 4.5 simulator.&lt;/p&gt;
&lt;h3 id="language-support"&gt;Language Support&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Declarations:&lt;/strong&gt; variables and constants (constants require an initializer).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Expressions:&lt;/strong&gt; &lt;code&gt;+&lt;/code&gt;, &lt;code&gt;-&lt;/code&gt;, &lt;code&gt;*&lt;/code&gt;, &lt;code&gt;/&lt;/code&gt;, unary minus, and the six relational operators (&lt;code&gt;==&lt;/code&gt;, &lt;code&gt;!=&lt;/code&gt;, &lt;code&gt;&amp;lt;&lt;/code&gt;, &lt;code&gt;&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;=&lt;/code&gt;, &lt;code&gt;&amp;gt;=&lt;/code&gt;), which are non-associative.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control flow:&lt;/strong&gt; &lt;code&gt;if&lt;/code&gt;, &lt;code&gt;if-else&lt;/code&gt;, &lt;code&gt;while&lt;/code&gt;, &lt;code&gt;do-while&lt;/code&gt; and &lt;code&gt;break&lt;/code&gt; (with correct handling of nested loops).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;I/O:&lt;/strong&gt; &lt;code&gt;print&lt;/code&gt; (integers and string literals) and &lt;code&gt;read&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="compiler-pipeline"&gt;Compiler Pipeline&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Lexer (Flex):&lt;/strong&gt; exclusive start conditions for multi-line comments and strings, longest-match ordering for keywords and two-character operators, identifier length limit, integer overflow check, and panic-mode recovery that reports one error per invalid sequence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parser (Bison):&lt;/strong&gt; operator precedence and associativity declared explicitly, dangling &lt;code&gt;else&lt;/code&gt; resolved with &lt;code&gt;%precedence&lt;/code&gt;, and a grammar with no unresolved conflicts (110 states).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error recovery:&lt;/strong&gt; synchronization on &lt;code&gt;;&lt;/code&gt; and &lt;code&gt;}&lt;/code&gt;, &lt;code&gt;%destructor&lt;/code&gt; rules to free discarded symbols, and a reset of the temporary-register pool after every recovery, which fixed false &amp;ldquo;no registers available&amp;rdquo; errors in cascading-error programs.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Semantic analysis:&lt;/strong&gt; program must be named &lt;code&gt;main&lt;/code&gt;, no redeclarations, constants cannot be reassigned or read into, no use of undeclared identifiers, and &lt;code&gt;break&lt;/code&gt; only inside loops. All errors are reported, not just the first.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code generation:&lt;/strong&gt; each main non-terminal carries a list of MIPS quadruples that is concatenated on every reduction, with a pool of ten temporary registers (&lt;code&gt;$t0&lt;/code&gt;-&lt;code&gt;$t9&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="optimizations"&gt;Optimizations&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Constant folding with lazy materialization:&lt;/strong&gt; expressions carry an attribute with a compile-time value and a flag. A constant is only turned into a &lt;code&gt;li&lt;/code&gt; instruction if it is actually used, so &lt;code&gt;3 + 4 * 2&lt;/code&gt; becomes a single &lt;code&gt;li $t0, 11&lt;/code&gt;. Division by zero in a constant expression produces a compile-time warning.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dead-code elimination:&lt;/strong&gt; conditions known at compile time remove branches entirely (&lt;code&gt;if (0)&lt;/code&gt;, &lt;code&gt;if (K)&lt;/code&gt;, &lt;code&gt;while (0)&lt;/code&gt;, and &lt;code&gt;if (3 &amp;gt; 5)&lt;/code&gt; after folding).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Peephole optimizer (separate module):&lt;/strong&gt; six patterns, including &lt;code&gt;add R, X, 0&lt;/code&gt; to &lt;code&gt;move&lt;/code&gt;, &lt;code&gt;mul R, X, 1&lt;/code&gt; to &lt;code&gt;move&lt;/code&gt;, &lt;code&gt;mul R, X, 0&lt;/code&gt; to &lt;code&gt;li R, 0&lt;/code&gt;, and multiplication by a power of two to &lt;code&gt;sll&lt;/code&gt;. It tracks the last known &lt;code&gt;li&lt;/code&gt; value per register across a basic block and stops at labels.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;String literal deduplication&lt;/strong&gt; in the &lt;code&gt;.data&lt;/code&gt; section through the symbol table.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="example"&gt;Example&lt;/h3&gt;
&lt;p&gt;Input:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-c" data-lang="c"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="n"&gt;var&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Generated MIPS (the initialization collapses to one instruction instead of five):&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-asm" data-lang="asm"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;li&lt;/span&gt; &lt;span class="no"&gt;$t0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;sw&lt;/span&gt; &lt;span class="no"&gt;$t0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;_x&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;lw&lt;/span&gt; &lt;span class="no"&gt;$t0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;_x&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;move&lt;/span&gt; &lt;span class="no"&gt;$a0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;$t0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;li&lt;/span&gt; &lt;span class="no"&gt;$v0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;syscall&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h3 id="testing"&gt;Testing&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Eleven small single-purpose test programs: valid programs for each feature (spec example, folding, dead code, peephole patterns, relational operators, &lt;code&gt;do-while&lt;/code&gt;, nested &lt;code&gt;break&lt;/code&gt;, a full integration program) and invalid programs for lexical, syntactic and semantic errors.&lt;/li&gt;
&lt;li&gt;A script checks the expected error counts per phase and, for valid programs, runs the generated assembly in MARS and checks the output.&lt;/li&gt;
&lt;li&gt;One command (&lt;code&gt;make test&lt;/code&gt;) builds the compiler and runs the whole suite.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="known-limitations"&gt;Known Limitations&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Constant folding does not propagate through declared constants (&lt;code&gt;const int c = 5; var int x = c * 2;&lt;/code&gt; is not folded).&lt;/li&gt;
&lt;li&gt;The peephole pass runs once instead of until a fixed point.&lt;/li&gt;
&lt;li&gt;The ten-register pool has no spilling.&lt;/li&gt;
&lt;li&gt;A few small dynamically allocated strings (labels and variable names) are not freed, because the provided list structure does not distinguish heap strings from literals.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="installation--execution"&gt;Installation &amp;amp; Execution&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Technical Requirements:&lt;/strong&gt; &lt;code&gt;gcc&lt;/code&gt;, &lt;code&gt;flex&lt;/code&gt;, &lt;code&gt;bison&lt;/code&gt;, &lt;code&gt;make&lt;/code&gt; on Linux; Java to run MARS 4.5.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Build and test:&lt;/strong&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;make
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;make &lt;span class="nb"&gt;test&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compile and run a program:&lt;/strong&gt;
&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-bash" data-lang="bash"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;./minic program.mc &amp;gt; program.s
&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;java -jar tests/Mars4_5.jar nc program.s
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id="tools--technologies"&gt;Tools &amp;amp; Technologies&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;C&lt;/li&gt;
&lt;li&gt;Flex&lt;/li&gt;
&lt;li&gt;Bison&lt;/li&gt;
&lt;li&gt;MIPS Assembly (MARS 4.5)&lt;/li&gt;
&lt;li&gt;Make&lt;/li&gt;
&lt;li&gt;Shell scripting&lt;/li&gt;
&lt;/ul&gt;</description></item></channel></rss>