Skip to content

Finish RISC-V TODO backlog: switch lowering, result returns, optimizer tweaks#41

Merged
mengzhuo merged 2 commits intoplctlab:mainfrom
DzmingLi:main
Nov 7, 2025
Merged

Finish RISC-V TODO backlog: switch lowering, result returns, optimizer tweaks#41
mengzhuo merged 2 commits intoplctlab:mainfrom
DzmingLi:main

Conversation

@DzmingLi
Copy link

@DzmingLi DzmingLi commented Oct 31, 2025

  • finish outstanding RISC-V TODOs: wrap Result-return codegen, add string/bytes switch
    lowering backed by interpreter memcmp, and improve register allocation plus JumpIndirect
    2-target lowering
  • expand optimizer/code quality work: eliminate unused pure calls, refactor inst_convert,
    extend integer switch support, and guard against conflicting default expressions
  • add dedicated regression suites covering result returns, string/bytes switches, pure call
    elimination, and wider switch type coverage

This commit resolves 5 TODO items in the MoonBit compiler, improving code
quality, optimization capabilities, and error handling. All changes are
original contributions not present in bmwangmh/workspace branch.

## 1. Pure Function Call Elimination (src/riscv_opt_peephole.ml:171)

- Implement dead code elimination for pure functions with unused results
- Improve is_pure function to support local alloca storage
- Add comprehensive test suite (test/src/pure_elim/)
- Optimization allows removal of side-effect-free calls, reducing code size

## 2. Refactor inst_convert Function (src/riscv_virtasm.ml:289)

- Reduce code duplication using typed helper functions
- Add explicit type annotations to prevent OCaml type inference issues
- Improve code structure with 10 conversion helpers for different instruction types
- Reduce repetitive pattern matching from 88 lines to organized, maintainable code

## 3. Extend Switch Statement Type Support (src/riscv_generate.ml:1311)

- Add support for C_byte, C_int64, C_uint, C_uint64 in switch statements
- Add clear TODO markers for unsupported types (float, double, bigint, string, bytes)
- Include test suite with future test cases documented (test/src/switch_types/)
- Prepare compiler for extended type support when MoonBit language adds these features

## 4. Optimize JumpIndirect (src/riscv_virtasm_generate.ml:377)

- Add validation for empty possibility list (fail fast with clear error)
- Preserve single-target optimization (convert to direct jump)
- Restructure to match expression for better clarity
- Document future optimization direction (conditional branches for 2-3 targets)

## 5. Handle default_expr Conflicts (src/ast_derive.ml:1996)

- Properly handle case where both positional and named layouts specify defaults
- Check for equality: accept if same, error with clear message if different
- Improve error reporting with parameter name/index in conflict messages
- Replace generic "TODO" failwith with semantic validation

## Test Coverage

- test/src/pure_elim/: Validates pure function elimination optimization
- test/src/switch_types/: Tests existing switch types, documents future additions
- All existing tests continue to pass
This commit implements 6 TODO items and adds comprehensive test coverage:

1. Error_result return handling (riscv_generate.ml:1171)
   - Wrap return values in Result<T, E> constructor
   - Handle both Ok (tag=1) and Err (tag=0) cases
   - Properly allocate and store tagged values

2. String constant switch support (riscv_generate.ml:1336)
   - Generate if-else chain with proper UTF-16 string comparison
   - Use memcmp for byte-by-byte comparison of UTF-16 LE encoded strings
   - First compare lengths, then compare content

3. Bytes constant switch support (riscv_generate.ml:1424)
   - Similar to string switch but for byte arrays
   - Each byte is 1 byte (not UTF-16 like strings)
   - Use memcmp for content comparison

4. Register allocation optimization (riscv_reg_alloc.ml:123)
   - Implement least-used register selection strategy
   - Replace arbitrary choice with usage-based heuristic
   - Reduce register pressure through better allocation

5. JumpIndirect optimization for 2 targets (riscv_virtasm_generate.ml:388)
   - Convert 2-target indirect jumps to conditional branches
   - Load target address and compare with jump register
   - Avoid indirect jump pipeline flush

6. Interpreter memcmp support (test/interpreter.cpp:365)
   - Add memcmp function to SSA interpreter
   - Required for string/bytes comparison to work in tests

Tests added:
- test/src/error_result: Tests Result<T,E> wrapping for error returns
- test/src/string_switch: Tests string pattern matching
- test/src/bytes_switch: Tests bytes pattern matching
@mengzhuo mengzhuo merged commit 1506d18 into plctlab:main Nov 7, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants