Skip to content

Add RISC-V target with zero runtime capability#3

Open
konard wants to merge 4 commits intouselessgoddess:masterfrom
konard:issue-2-f4cafc8b0b38
Open

Add RISC-V target with zero runtime capability#3
konard wants to merge 4 commits intouselessgoddess:masterfrom
konard:issue-2-f4cafc8b0b38

Conversation

@konard
Copy link

@konard konard commented Nov 5, 2025

Summary

This PR implements RISC-V target support with zero runtime capability as requested in issue #2.

Features Implemented

RISC-V 32-bit target triple (riscv32i-unknown-none-elf)

  • Supports compilation to bin, dylib, and staticlib output types
  • Uses bare metal environment (os = "none")
  • Configured for RISC-V 32-bit architecture with appropriate data layout
  • Suitable for embedded systems and zero-runtime environments

Optional entry point via -Z no_main flag

  • When enabled, entry_fn returns None
  • Allows compilation without main or #[start] functions
  • Enables building libraries and runtime-less code

#[no_mangle] attribute support

  • Functions marked with #[no_mangle] preserve their original symbol names
  • Symbol mangling is skipped for such functions
  • Essential for FFI and bare metal programming

Implementation Details

New Files

  • compiler/middle/src/spec/base/none.rs - Bare metal base configuration
  • compiler/middle/src/spec/targets/riscv32i_unknown_none_elf.rs - RISC-V target specification
  • experiments/test_riscv_*.zxc - Test files demonstrating functionality

Modified Files

  • compiler/middle/src/symbol.rs - Added no_mangle symbol
  • compiler/middle/src/man/mod.rs - Skip mangling for #[no_mangle] functions
  • compiler/middle/src/hir/mod.rs - Respect no_main flag in entry_fn()
  • compiler/middle/src/sess/mod.rs - Added -Z no_main compiler option
  • compiler/middle/src/spec/mod.rs - Registered new RISC-V target

Additional Changes

  • Updated project to Rust 2024 edition for compatibility with let chains
  • Removed stabilized feature flags

Usage Examples

Compile for RISC-V with no main function

zxc --target riscv32i-unknown-none-elf -Z no_main --crate-type staticlib input.zxc

Use #[no_mangle] attribute

#[no_mangle]
fn my_function() -> i32 {
    42
}

Testing

Test files have been added to the experiments/ directory demonstrating:

  1. Zero-runtime code with #[no_mangle] functions
  2. Mixed code with both #[no_mangle] functions and a main entry point

Related

Fixes #2


🤖 Generated with Claude Code

konard and others added 3 commits November 5, 2025 12:21
Adding CLAUDE.md with task information for AI processing.
This file will be removed when the task is complete.

Issue: uselessgoddess#2
Implements the following features as requested in issue uselessgoddess#2:

1. Add riscv32i-unknown-none-elf target triple
   - Supports compilation to bin, dylib, and staticlib output types
   - Uses bare metal environment (os = "none")
   - Configured for RISC-V 32-bit architecture with appropriate data layout
   - Supports zero-runtime environments suitable for embedded systems

2. Allow entry_fn to return None when no_main option is set
   - Added no_main compiler option (-Z no_main)
   - When enabled, the compiler doesn't require a main or start function
   - Enables compilation of libraries and runtime-less code

3. Add no_mangle attribute support for functions
   - Added no_mangle to symbol table
   - Modified compute_symbol_name to skip mangling when #[no_mangle] is present
   - Functions marked with #[no_mangle] preserve their original symbol names

Implementation details:
- Created compiler/middle/src/spec/base/none.rs for bare metal base configuration
- Created compiler/middle/src/spec/targets/riscv32i_unknown_none_elf.rs target spec
- Added no_main flag to CompilerOptions (Z flags)
- Modified HirCtx::entry_fn to respect no_main flag
- Updated symbol mangling logic in man/mod.rs
- Added test files in experiments/ directory

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
- Updated all compiler crates to edition = "2024"
- Removed stabilized let_chains from feature flags in lexer
- Required for building with latest Rust nightly

This change is necessary for the codebase to build with current nightly Rust versions,
as let chains require Rust 2024 edition or explicit feature gate.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@konard konard changed the title [WIP] RISC-V target with zero runtime capability Add RISC-V target with zero runtime capability Nov 5, 2025
@konard konard marked this pull request as ready for review November 5, 2025 11:38
@konard
Copy link
Author

konard commented Nov 5, 2025

🤖 Solution Draft Log

This log file contains the complete execution trace of the AI solution draft process.

💰 Cost estimation:

  • Public pricing estimate: $3.733992 USD
  • Calculated by Anthropic: $2.210030 USD
  • Difference: $-1.523962 (-40.81%)
    📎 Log file uploaded as GitHub Gist (416KB)
    🔗 View complete solution draft log

Now working session is ended, feel free to review and add any feedback on the solution draft.

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.

RISC-V target with zero runtime capability

1 participant