MCPcopy Create free account
hub / github.com/LPC4/Full-Stack / parse_instruction_line

Function parse_instruction_line

crates/asm-to-binary/src/assembler/parser.rs:152–317  ·  view source on GitHub ↗

Parse a trimmed instruction line and push typed `AsmToken`s into `out`. May push more than one token (e.g. `li` expands to up to two real instructions). Unrecognised mnemonics are emitted as `AsmToken::Comment` so nothing is silently lost.

(line: &str, out: &mut Vec<AsmToken>)

Source from the content-addressed store, hash-verified

150 Directive::Space(n) => out.push(AsmToken::Space(n)),
151 Directive::Equ(_, _) | Directive::Unknown(_) => {}
152 }
153}
154
155// --- Parsing raw instruction lines ---
156
157// May push more than one token (e.g. `li` expands to up to two real instructions).
158// Unrecognised mnemonics are emitted as `AsmToken::Comment` so nothing is silently lost.
159fn parse_instruction_line(line: &str, out: &mut Vec<AsmToken>) {
160 // Strip inline `;` or `#` comments before any operand parsing.
161 let line = match line.find([';', '#']) {
162 Some(i) => line[..i].trim_end(),
163 None => line,
164 };
165 if line.is_empty() {
166 return;
167 }
168 let (mnemonic, rest) = split_mnemonic(line);
169
170 // --- Dispatch by mnemonic ---
171
172 // Branch instructions (`bne`, `beq`, `blt`, `bge`, `bltu`, `bgeu`)
173 if let Some(kind) = BranchKind::from_mnemonic(mnemonic) {
174 if let Some(tok) = parse_branch(kind, rest) {
175 out.push(tok);
176 }
177 return;
178 }
179
180 match mnemonic {
181 // --- Symbol-bearing pseudos ---
182 "call" => {
183 let sym = rest.trim().to_owned();
184 if !sym.is_empty() {
185 out.push(AsmToken::Call { symbol: sym });
186 }
187 }
188 "tail" => {
189 let sym = rest.trim().to_owned();
190 if !sym.is_empty() {
191 out.push(AsmToken::Tail { symbol: sym });
192 }
193 }
194 "j" => {
195 let target = rest.trim().to_owned();
196 if !target.is_empty() {
197 out.push(AsmToken::Jal { rd: 0, target });
198 }
199 }
200 "jal" => try_parse_or_warn!(out, line, "jal", parse_jal(rest)),
201 "la" => try_parse_or_warn!(out, line, "la", parse_la(rest)),
202 "jalr" => {
203 if let Some((rd, rs1, imm)) = parse_load_mem(rest) {
204 out.push(AsmToken::Real(RealInstruction::Jalr(Jalr::new(
205 rd, rs1, imm,
206 ))));
207 } else {
208 asm_warn!(out, "unrecognised jalr: {line}");
209 }

Callers 2

encode_lineFunction · 0.85

Calls 15

split_mnemonicFunction · 0.85
parse_branchFunction · 0.85
EcallClass · 0.85
WfiClass · 0.85
JalrClass · 0.85
MretClass · 0.85
SretClass · 0.85
SfenceVmaClass · 0.85
try_parse_liFunction · 0.85
parse_two_regsFunction · 0.85
parse_store_memFunction · 0.85
store_instFunction · 0.85

Tested by

no test coverage detected