Files
hakorune/docs/予定/native-plan/llvm/issue/MIR-Quick-Reference.md
Moe Charm f4988985a7 docs: Phase 9.78 LLVM PoC - AI大会議結果とCopilot実装ガイド作成
- AI大会議(Gemini/Codex)でLLVM実装戦略を策定
- inkwellクレート使用、Box型はptr型で表現、ランタイム委譲戦略
- Week 1: Hello World(return 42)の実装から開始
- 3週間で基本動作確認、目標10倍以上の性能向上
- Copilot用の詳細な実装ガイドとIssueテンプレート作成

Phase 8.6 VM性能改善(50.94倍高速化)完了を受けて、
次なる性能革命としてLLVMバックエンド実装を開始!

🚀 Ready to make Nyash fly with LLVM!
2025-08-18 17:51:31 +09:00

159 lines
4.0 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 📚 MIR クイックリファレンス for LLVM実装
## 🎯 Week 1で対応するMIR命令
### 1. **Const命令**
```rust
// MIR表現
MirInstruction::Const(value_id, constant_value)
// 例
Const(v1, MirConstant::Integer(42))
Const(v2, MirConstant::Float(3.14))
Const(v3, MirConstant::Bool(true))
// LLVM変換
let int_val = ctx.i32_type().const_int(42, false);
let float_val = ctx.f64_type().const_float(3.14);
let bool_val = ctx.bool_type().const_int(1, false);
```
### 2. **Return命令**
```rust
// MIR表現
MirInstruction::Return(Option<ValueId>)
// 例
Return(Some(v1)) // 値を返す
Return(None) // voidを返す
// LLVM変換
builder.build_return(Some(&value));
builder.build_return(None);
```
## 📄 参考: 現在のMIR構造
```rust
// src/mir/instruction.rs の主要部分
pub enum MirInstruction {
// Week 1対象
Const(ValueId, MirConstant),
Return(Option<ValueId>),
// Week 2対象
BinOp(ValueId, BinaryOp, ValueId, ValueId),
Compare(ValueId, CompareOp, ValueId, ValueId),
Branch(ValueId, BasicBlockId, BasicBlockId),
Jump(BasicBlockId),
// Week 3以降
BoxNew(ValueId, MirType),
BoxCall(ValueId, ValueId, String, Vec<ValueId>),
// ... 他の命令
}
// 定数の型
pub enum MirConstant {
Integer(i64),
Float(f64),
Bool(bool),
String(String),
Null,
}
```
## 🔄 MIR→LLVM変換の基本パターン
```rust
// 基本的な変換ループ
for instruction in &block.instructions {
match instruction {
MirInstruction::Const(value_id, constant) => {
let llvm_value = match constant {
MirConstant::Integer(n) => {
ctx.i64_type().const_int(*n as u64, true).into()
}
MirConstant::Float(f) => {
ctx.f64_type().const_float(*f).into()
}
MirConstant::Bool(b) => {
ctx.bool_type().const_int(*b as u64, false).into()
}
_ => todo!("Other constants"),
};
// value_idとllvm_valueをマッピングに保存
value_map.insert(*value_id, llvm_value);
}
MirInstruction::Return(value_id) => {
match value_id {
Some(id) => {
let value = value_map.get(id).unwrap();
builder.build_return(Some(value));
}
None => {
builder.build_return(None);
}
}
}
_ => {} // Week 1では他の命令は無視
}
}
```
## 🎯 テスト用のMIRサンプル
### 1. **return 42のMIR**
```rust
MirModule {
functions: vec![
MirFunction {
name: "Main.main",
params: vec![],
return_type: MirType::Integer,
blocks: vec![
BasicBlock {
id: 0,
instructions: vec![
Const(v1, MirConstant::Integer(42)),
Return(Some(v1)),
],
},
],
},
],
}
```
### 2. **簡単な計算のMIR**Week 2用
```rust
// return 10 + 5
BasicBlock {
instructions: vec![
Const(v1, MirConstant::Integer(10)),
Const(v2, MirConstant::Integer(5)),
BinOp(v3, BinaryOp::Add, v1, v2),
Return(Some(v3)),
],
}
```
## 💡 実装のヒント
1. **ValueIdマッピング**: `HashMap<ValueId, BasicValueEnum>`で管理
2. **型情報**: MIRは型情報を持つので、LLVM型への変換テーブルを作る
3. **基本ブロック**: MIRのBasicBlockIdをLLVMのBasicBlockにマッピング
4. **エラー処理**: 最初は`todo!()`でOK、後から実装
## 📁 関連ファイル
- MIR定義: `src/mir/instruction.rs`
- MIR生成: `src/mir/lowering.rs`
- 参考実装: `src/backend/vm.rs`VMのMIR処理
---
**注**: このリファレンスはWeek 1の実装に必要な最小限の情報です。
詳細は実際のソースコードを参照してください。