Skip to main content

clr_assembler/program/
mod.rs

1use gaia_types::{GaiaError, Result, SourceLocation};
2use oak_msil::ast as msil;
3use std::collections::HashMap;
4
5/// CLR 程序的高层语义信息结构
6///
7/// 该结构体表示一个完整的 .NET 程序集,包含了运行时所需的所有信息。
8/// 采用与 wasi-assembler 和 jvm-assembler 相同的架构设计。
9#[derive(Debug, Clone)]
10pub struct ClrProgram {
11    /// 程序集名称
12    pub name: String,
13    /// 程序集版本信息
14    pub version: ClrVersion,
15    /// 访问标志
16    pub access_flags: ClrAccessFlags,
17    /// 外部程序集引用
18    pub external_assemblies: Vec<ClrExternalAssembly>,
19    /// 模块信息
20    pub module: Option<ClrModule>,
21    /// 类型定义列表
22    pub types: Vec<ClrType>,
23    /// 全局方法列表
24    pub global_methods: Vec<ClrMethod>,
25    /// 全局字段列表
26    pub global_fields: Vec<ClrField>,
27    /// 属性列表
28    pub attributes: Vec<ClrAttribute>,
29    /// 常量池(字符串、GUID、Blob 等)
30    pub constant_pool: ClrConstantPool,
31    /// 源文件信息
32    pub source_file: Option<String>,
33}
34
35/// CLR 版本信息
36#[derive(Debug, Clone, Copy, PartialEq)]
37pub struct ClrVersion {
38    /// 主版本号
39    pub major: u16,
40    /// 次版本号
41    pub minor: u16,
42    /// 构建号
43    pub build: u16,
44    /// 修订号
45    pub revision: u16,
46}
47
48/// 访问标志
49#[derive(Debug, Clone, Copy, PartialEq)]
50pub struct ClrAccessFlags {
51    /// 是否为公共程序集
52    pub is_public: bool,
53    /// 是否为私有程序集
54    pub is_private: bool,
55    /// 是否启用安全透明
56    pub is_security_transparent: bool,
57    /// 是否为可重定位程序集
58    pub is_retargetable: bool,
59}
60
61/// 外部程序集引用
62#[derive(Debug, Clone)]
63pub struct ClrExternalAssembly {
64    /// 程序集名称
65    pub name: String,
66    /// 版本信息
67    pub version: ClrVersion,
68    /// 公钥标记
69    pub public_key_token: Option<Vec<u8>>,
70    /// 文化信息
71    pub culture: Option<String>,
72    /// 哈希算法
73    pub hash_algorithm: Option<u32>,
74}
75
76/// 模块信息
77#[derive(Debug, Clone)]
78pub struct ClrModule {
79    /// 模块名称
80    pub name: String,
81    /// 模块版本 ID
82    pub mvid: Option<Vec<u8>>,
83}
84
85/// CLR 类型定义
86#[derive(Debug, Clone)]
87pub struct ClrType {
88    /// 类型名称
89    pub name: String,
90    /// 命名空间
91    pub namespace: Option<String>,
92    /// 访问标志
93    pub access_flags: ClrAccessFlags,
94    /// 基类型
95    pub base_type: Option<String>,
96    /// 实现的接口
97    pub interfaces: Vec<String>,
98    /// 字段列表
99    pub fields: Vec<ClrField>,
100    /// 方法列表
101    pub methods: Vec<ClrMethod>,
102    /// 属性列表
103    pub properties: Vec<ClrProperty>,
104    /// 事件列表
105    pub events: Vec<ClrEvent>,
106    /// 嵌套类型
107    pub nested_types: Vec<ClrType>,
108    /// 属性
109    pub attributes: Vec<ClrAttribute>,
110}
111
112/// CLR 方法定义
113#[derive(Debug, Clone)]
114pub struct ClrMethod {
115    /// 方法名称
116    pub name: String,
117    /// 返回类型
118    pub return_type: ClrTypeReference,
119    /// 参数列表
120    pub parameters: Vec<ClrParameter>,
121    /// 访问标志
122    pub access_flags: ClrAccessFlags,
123    /// 方法实现标志
124    pub impl_flags: ClrMethodImplFlags,
125    /// 指令列表
126    pub instructions: Vec<ClrInstruction>,
127    /// 最大栈深度
128    pub max_stack: u16,
129    /// 局部变量
130    pub locals: Vec<ClrLocalVariable>,
131    /// 异常处理表
132    pub exception_handlers: Vec<ClrExceptionHandler>,
133    /// 属性
134    pub attributes: Vec<ClrAttribute>,
135    /// 是否为入口点
136    pub is_entry_point: bool,
137}
138
139/// CLR 字段定义
140#[derive(Debug, Clone)]
141pub struct ClrField {
142    /// 字段名称
143    pub name: String,
144    /// 字段类型
145    pub field_type: ClrTypeReference,
146    /// 访问标志
147    pub access_flags: ClrAccessFlags,
148    /// 默认值
149    pub default_value: Option<ClrConstantValue>,
150    /// 属性
151    pub attributes: Vec<ClrAttribute>,
152}
153
154/// CLR 属性定义
155#[derive(Debug, Clone)]
156pub struct ClrProperty {
157    /// 属性名称
158    pub name: String,
159    /// 属性类型
160    pub property_type: ClrTypeReference,
161    /// Getter 方法
162    pub getter: Option<String>,
163    /// Setter 方法
164    pub setter: Option<String>,
165    /// 属性
166    pub attributes: Vec<ClrAttribute>,
167}
168
169/// CLR 事件定义
170#[derive(Debug, Clone)]
171pub struct ClrEvent {
172    /// 事件名称
173    pub name: String,
174    /// 事件类型
175    pub event_type: ClrTypeReference,
176    /// 添加方法
177    pub add_method: Option<String>,
178    /// 移除方法
179    pub remove_method: Option<String>,
180    /// 触发方法
181    pub raise_method: Option<String>,
182    /// 属性
183    pub attributes: Vec<ClrAttribute>,
184}
185
186/// CLR 指令
187#[derive(Debug, Clone, PartialEq)]
188pub enum ClrInstruction {
189    /// 简单指令(无操作数)
190    Simple { opcode: ClrOpcode },
191    /// 带立即数的指令
192    WithImmediate { opcode: ClrOpcode, value: i32 },
193    /// 带 64 位立即数的指令
194    WithImmediate64 { opcode: ClrOpcode, value: i64 },
195    /// 带 32 位浮点数的指令
196    WithFloat32 { opcode: ClrOpcode, value: f32 },
197    /// 带 64 位浮点数的指令
198    WithFloat64 { opcode: ClrOpcode, value: f64 },
199    /// 带局部变量索引的指令
200    WithLocalVar { opcode: ClrOpcode, index: u16 },
201    /// 带参数索引的指令
202    WithParameter { opcode: ClrOpcode, index: u16 },
203    /// 带字段引用的指令
204    WithField { opcode: ClrOpcode, field_ref: String },
205    /// 带方法引用的指令
206    WithMethod { opcode: ClrOpcode, method_ref: String },
207    /// 带类型引用的指令
208    WithType { opcode: ClrOpcode, type_ref: String },
209    /// 带字符串的指令
210    WithString { opcode: ClrOpcode, value: String },
211    /// 带分支标签的指令
212    WithLabel { opcode: ClrOpcode, label: String },
213    /// 带 switch 表的指令
214    WithSwitch { opcode: ClrOpcode, labels: Vec<String> },
215}
216
217/// CLR 操作码
218#[derive(Debug, Clone, Copy, PartialEq)]
219pub enum ClrOpcode {
220    // 常量加载指令
221    Nop,
222    LdcI4,
223    LdcI4S,
224    LdcI4M1,
225    LdcI40,
226    LdcI41,
227    LdcI42,
228    LdcI43,
229    LdcI44,
230    LdcI45,
231    LdcI46,
232    LdcI47,
233    LdcI48,
234    LdcI8,
235    LdcR4,
236    LdcR8,
237    Ldnull,
238    Ldstr,
239
240    // 参数和局部变量指令
241    Ldarg,
242    LdargS,
243    Ldarg0,
244    Ldarg1,
245    Ldarg2,
246    Ldarg3,
247    Ldloc,
248    LdlocS,
249    Ldloc0,
250    Ldloc1,
251    Ldloc2,
252    Ldloc3,
253    Starg,
254    StargS,
255    Stloc,
256    StlocS,
257    Stloc0,
258    Stloc1,
259    Stloc2,
260    Stloc3,
261    Ldarga,
262    LdargaS,
263    Ldloca,
264    LdlocaS,
265
266    // 数组指令
267    Ldelem,
268    LdelemI1,
269    LdelemU1,
270    LdelemI2,
271    LdelemU2,
272    LdelemI4,
273    LdelemU4,
274    LdelemI8,
275    LdelemI,
276    LdelemR4,
277    LdelemR8,
278    LdelemRef,
279    Stelem,
280    StelemI1,
281    StelemI2,
282    StelemI4,
283    StelemI8,
284    StelemI,
285    StelemR4,
286    StelemR8,
287    StelemRef,
288    Ldlen,
289    Newarr,
290
291    // 间接加载/存储指令
292    LdindI1,
293    LdindU1,
294    LdindI2,
295    LdindU2,
296    LdindI4,
297    LdindU4,
298    LdindI8,
299    LdindI,
300    LdindR4,
301    LdindR8,
302    LdindRef,
303    StindI1,
304    StindI2,
305    StindI4,
306    StindI8,
307    StindI,
308    StindR4,
309    StindR8,
310    StindRef,
311
312    // 字段指令
313    Ldfld,
314    Ldflda,
315    Stfld,
316    Ldsfld,
317    Ldsflda,
318    Stsfld,
319
320    // 方法调用指令
321    Call,
322    Callvirt,
323    Calli,
324    Ret,
325
326    // 对象指令
327    Newobj,
328    Castclass,
329    Isinst,
330    Unbox,
331    UnboxAny,
332    Box,
333
334    // 控制流指令
335    Br,
336    BrS,
337    Brtrue,
338    BrtrueS,
339    Brfalse,
340    BrfalseS,
341    Beq,
342    BeqS,
343    Bne,
344    BneS,
345    Blt,
346    BltS,
347    BltUn,
348    BltUnS,
349    Ble,
350    BleS,
351    BleUn,
352    BleUnS,
353    Bgt,
354    BgtS,
355    BgtUn,
356    BgtUnS,
357    Bge,
358    BgeS,
359    BgeUn,
360    BgeUnS,
361    Switch,
362
363    // 算术指令
364    Add,
365    AddOvf,
366    AddOvfUn,
367    Sub,
368    SubOvf,
369    SubOvfUn,
370    Mul,
371    MulOvf,
372    MulOvfUn,
373    Div,
374    DivUn,
375    Rem,
376    RemUn,
377    And,
378    Or,
379    Xor,
380    Not,
381    Shl,
382    Shr,
383    ShrUn,
384    Neg,
385
386    // 比较指令
387    Ceq,
388    Cgt,
389    CgtUn,
390    Clt,
391    CltUn,
392
393    // 转换指令
394    ConvI1,
395    ConvI2,
396    ConvI4,
397    ConvI8,
398    ConvR4,
399    ConvR8,
400    ConvU4,
401    ConvU8,
402    ConvOvfI1,
403    ConvOvfI2,
404    ConvOvfI4,
405    ConvOvfI8,
406    ConvOvfU1,
407    ConvOvfU2,
408    ConvOvfU4,
409    ConvOvfU8,
410    ConvOvfI1Un,
411    ConvOvfI2Un,
412    ConvOvfI4Un,
413    ConvOvfI8Un,
414    ConvOvfU1Un,
415    ConvOvfU2Un,
416    ConvOvfU4Un,
417    ConvOvfU8Un,
418    ConvRUn,
419    ConvOvfIUn,
420    ConvOvfUUn,
421
422    // 栈操作指令
423    Dup,
424    Pop,
425
426    // 异常处理指令
427    Throw,
428    Rethrow,
429    Leave,
430    LeaveS,
431    Endfinally,
432    Endfilter,
433
434    // 其他指令
435    Sizeof,
436    Refanytype,
437    Refanyval,
438    Mkrefany,
439    Arglist,
440    Localloc,
441    Jmp,
442    Calli2,
443    Tail,
444    Volatile,
445    Unaligned,
446    Constrained,
447    Readonly,
448}
449
450/// CLR 类型引用
451#[derive(Debug, Clone, PartialEq)]
452pub struct ClrTypeReference {
453    /// 类型名称
454    pub name: String,
455    /// 命名空间
456    pub namespace: Option<String>,
457    /// 程序集引用
458    pub assembly: Option<String>,
459    /// 是否为值类型
460    pub is_value_type: bool,
461    /// 是否为引用类型
462    pub is_reference_type: bool,
463    /// 泛型参数
464    pub generic_parameters: Vec<ClrTypeReference>,
465}
466
467/// CLR 参数
468#[derive(Debug, Clone)]
469pub struct ClrParameter {
470    /// 参数名称
471    pub name: String,
472    /// 参数类型
473    pub parameter_type: ClrTypeReference,
474    /// 是否为输入参数
475    pub is_in: bool,
476    /// 是否为输出参数
477    pub is_out: bool,
478    /// 是否为可选参数
479    pub is_optional: bool,
480    /// 默认值
481    pub default_value: Option<ClrConstantValue>,
482    /// 属性
483    pub attributes: Vec<ClrAttribute>,
484}
485
486/// CLR 局部变量
487#[derive(Debug, Clone)]
488pub struct ClrLocalVariable {
489    /// 变量名称(可选)
490    pub name: Option<String>,
491    /// 变量类型
492    pub variable_type: ClrTypeReference,
493    /// 是否为固定变量
494    pub is_pinned: bool,
495}
496
497/// CLR 异常处理器
498#[derive(Debug, Clone)]
499pub struct ClrExceptionHandler {
500    /// 处理器类型
501    pub handler_type: ClrExceptionHandlerType,
502    /// 尝试块开始偏移
503    pub try_start: u32,
504    /// 尝试块长度
505    pub try_length: u32,
506    /// 处理器开始偏移
507    pub handler_start: u32,
508    /// 处理器长度
509    pub handler_length: u32,
510    /// 异常类型(对于 catch 处理器)
511    pub catch_type: Option<ClrTypeReference>,
512    /// 过滤器开始偏移(对于 filter 处理器)
513    pub filter_start: Option<u32>,
514}
515
516/// CLR 异常处理器类型
517#[derive(Debug, Clone, Copy, PartialEq)]
518pub enum ClrExceptionHandlerType {
519    /// Catch 处理器
520    Catch,
521    /// Filter 处理器
522    Filter,
523    /// Finally 处理器
524    Finally,
525    /// Fault 处理器
526    Fault,
527}
528
529/// CLR 方法实现标志
530#[derive(Debug, Clone, Copy, PartialEq)]
531pub struct ClrMethodImplFlags {
532    /// 是否为托管代码
533    pub is_managed: bool,
534    /// 是否为本机代码
535    pub is_native: bool,
536    /// 是否为运行时代码
537    pub is_runtime: bool,
538    /// 是否为内联
539    pub is_inline: bool,
540    /// 是否不内联
541    pub is_no_inline: bool,
542    /// 是否同步
543    pub is_synchronized: bool,
544}
545
546/// CLR 属性
547#[derive(Debug, Clone)]
548pub struct ClrAttribute {
549    /// 属性类型
550    pub attribute_type: ClrTypeReference,
551    /// 构造函数参数
552    pub constructor_args: Vec<ClrConstantValue>,
553    /// 命名参数
554    pub named_args: Vec<(String, ClrConstantValue)>,
555}
556
557/// CLR 常量值
558#[derive(Debug, Clone, PartialEq)]
559pub enum ClrConstantValue {
560    /// 布尔值
561    Boolean(bool),
562    /// 8 位有符号整数
563    I1(i8),
564    /// 8 位无符号整数
565    U1(u8),
566    /// 16 位有符号整数
567    I2(i16),
568    /// 16 位无符号整数
569    U2(u16),
570    /// 32 位有符号整数
571    I4(i32),
572    /// 32 位无符号整数
573    U4(u32),
574    /// 64 位有符号整数
575    I8(i64),
576    /// 64 位无符号整数
577    U8(u64),
578    /// 32 位浮点数
579    R4(f32),
580    /// 64 位浮点数
581    R8(f64),
582    /// 字符串
583    String(String),
584    /// 空值
585    Null,
586    /// 类型引用
587    Type(ClrTypeReference),
588    /// 枚举值
589    Enum(ClrTypeReference, Box<ClrConstantValue>),
590    /// 数组
591    Array(Vec<ClrConstantValue>),
592}
593
594/// CLR 常量池
595#[derive(Debug, Clone)]
596pub struct ClrConstantPool {
597    /// 字符串表
598    pub strings: HashMap<String, u32>,
599    /// GUID 表
600    pub guids: HashMap<Vec<u8>, u32>,
601    /// Blob 表
602    pub blobs: HashMap<Vec<u8>, u32>,
603    /// 用户字符串表
604    pub user_strings: HashMap<String, u32>,
605}
606
607impl ClrProgram {
608    /// 创建新的 CLR 程序
609    pub fn new(name: impl Into<String>) -> Self {
610        Self {
611            name: name.into(),
612            version: ClrVersion::default(),
613            access_flags: ClrAccessFlags::default(),
614            external_assemblies: Vec::new(),
615            module: None,
616            types: Vec::new(),
617            global_methods: Vec::new(),
618            global_fields: Vec::new(),
619            attributes: Vec::new(),
620            constant_pool: ClrConstantPool::new(),
621            source_file: None,
622        }
623    }
624
625    /// 添加类型
626    pub fn add_type(&mut self, clr_type: ClrType) {
627        self.types.push(clr_type);
628    }
629
630    /// 添加外部程序集引用
631    pub fn add_external_assembly(&mut self, assembly: ClrExternalAssembly) {
632        self.external_assemblies.push(assembly);
633    }
634
635    /// 设置模块信息
636    pub fn set_module(&mut self, module: ClrModule) {
637        self.module = Some(module);
638    }
639
640    /// 设置源文件
641    pub fn set_source_file(&mut self, filename: String) {
642        self.source_file = Some(filename);
643    }
644
645    /// 验证程序的完整性
646    pub fn validate(&self) -> Result<()> {
647        // 验证程序集名称
648        if self.name.is_empty() {
649            return Err(GaiaError::syntax_error("程序集名称不能为空".to_string(), SourceLocation::default()));
650        }
651
652        // 验证类型定义
653        for clr_type in &self.types {
654            clr_type.validate()?;
655        }
656
657        Ok(())
658    }
659
660    /// 获取类型数量
661    pub fn get_type_count(&self) -> usize {
662        self.types.len()
663    }
664
665    /// 获取方法数量(包括全局方法和类型中的方法)
666    pub fn get_method_count(&self) -> usize {
667        let type_methods: usize = self.types.iter().map(|t| t.methods.len()).sum();
668        type_methods + self.global_methods.len()
669    }
670
671    /// 获取字段数量(包括全局字段和类型中的字段)
672    pub fn get_field_count(&self) -> usize {
673        let type_fields: usize = self.types.iter().map(|t| t.fields.len()).sum();
674        type_fields + self.global_fields.len()
675    }
676
677    /// 获取示例类型名称
678    pub fn get_sample_type_name(&self) -> Option<String> {
679        self.types.first().map(|t| {
680            if let Some(namespace) = &t.namespace {
681                format!("{}.{}", namespace, t.name)
682            }
683            else {
684                t.name.clone()
685            }
686        })
687    }
688
689    /// 获取示例方法名称
690    pub fn get_sample_method_name(&self) -> Option<String> {
691        // 首先尝试从类型中获取方法
692        for clr_type in &self.types {
693            if let Some(method) = clr_type.methods.first() {
694                return Some(format!("{}.{}", clr_type.name, method.name));
695            }
696        }
697        // 如果没有类型方法,尝试全局方法
698        self.global_methods.first().map(|m| m.name.clone())
699    }
700
701    /// 获取引用的程序集列表
702    pub fn get_referenced_assemblies(&self) -> Vec<String> {
703        self.external_assemblies.iter().map(|a| a.name.clone()).collect()
704    }
705
706    /// 转换为 MSIL AST
707    pub fn to_msil(&self) -> msil::MsilRoot {
708        let mut items = Vec::new();
709
710        // 添加外部程序集引用
711        for ext in &self.external_assemblies {
712            items.push(msil::Item::AssemblyExtern(ext.name.clone()));
713        }
714
715        // 添加程序集定义
716        items.push(msil::Item::Assembly(msil::Assembly { name: self.name.clone(), span: (0..0usize).into() }));
717
718        // 添加模块定义
719        if let Some(module) = &self.module {
720            items.push(msil::Item::Module(module.name.clone()));
721        }
722
723        // 添加类型定义
724        for clr_type in &self.types {
725            items.push(msil::Item::Class(clr_type.to_msil()));
726        }
727
728        msil::MsilRoot { items }
729    }
730}
731
732impl ClrType {
733    fn to_msil(&self) -> msil::Class {
734        msil::Class {
735            name: self.name.clone(),
736            methods: self.methods.iter().map(|m| m.to_msil()).collect(),
737            span: (0..0usize).into(),
738        }
739    }
740}
741
742impl Default for ClrTypeReference {
743    fn default() -> Self {
744        Self {
745            name: "void".to_string(),
746            namespace: None,
747            assembly: None,
748            is_value_type: false,
749            is_reference_type: true,
750            generic_parameters: Vec::new(),
751        }
752    }
753}
754
755impl ClrTypeReference {
756    /// 创建新的类型引用
757    pub fn new(name: String) -> Self {
758        Self { name, ..Default::default() }
759    }
760}
761
762impl ClrMethod {
763    fn to_msil(&self) -> msil::Method {
764        msil::Method {
765            name: self.name.clone(),
766            instructions: self.instructions.iter().map(|i| i.to_msil()).collect(),
767            span: (0..0usize).into(),
768        }
769    }
770}
771
772impl ClrInstruction {
773    fn to_msil(&self) -> msil::Instruction {
774        match self {
775            ClrInstruction::Simple { opcode } => {
776                msil::Instruction { opcode: format!("{:?}", opcode).to_lowercase(), operand: None, span: (0..0usize).into() }
777            }
778            ClrInstruction::WithImmediate { opcode, value } => msil::Instruction {
779                opcode: format!("{:?}", opcode).to_lowercase(),
780                operand: Some(value.to_string()),
781                span: (0..0usize).into(),
782            },
783            ClrInstruction::WithImmediate64 { opcode, value } => msil::Instruction {
784                opcode: format!("{:?}", opcode).to_lowercase(),
785                operand: Some(value.to_string()),
786                span: (0..0usize).into(),
787            },
788            ClrInstruction::WithFloat32 { opcode, value } => msil::Instruction {
789                opcode: format!("{:?}", opcode).to_lowercase(),
790                operand: Some(value.to_string()),
791                span: (0..0usize).into(),
792            },
793            ClrInstruction::WithFloat64 { opcode, value } => msil::Instruction {
794                opcode: format!("{:?}", opcode).to_lowercase(),
795                operand: Some(value.to_string()),
796                span: (0..0usize).into(),
797            },
798            ClrInstruction::WithLocalVar { opcode, index } => msil::Instruction {
799                opcode: format!("{:?}", opcode).to_lowercase(),
800                operand: Some(index.to_string()),
801                span: (0..0usize).into(),
802            },
803            ClrInstruction::WithParameter { opcode, index } => msil::Instruction {
804                opcode: format!("{:?}", opcode).to_lowercase(),
805                operand: Some(index.to_string()),
806                span: (0..0usize).into(),
807            },
808            ClrInstruction::WithField { opcode, field_ref } => msil::Instruction {
809                opcode: format!("{:?}", opcode).to_lowercase(),
810                operand: Some(field_ref.clone()),
811                span: (0..0usize).into(),
812            },
813            ClrInstruction::WithMethod { opcode, method_ref } => msil::Instruction {
814                opcode: format!("{:?}", opcode).to_lowercase(),
815                operand: Some(method_ref.clone()),
816                span: (0..0usize).into(),
817            },
818            ClrInstruction::WithType { opcode, type_ref } => msil::Instruction {
819                opcode: format!("{:?}", opcode).to_lowercase(),
820                operand: Some(type_ref.clone()),
821                span: (0..0usize).into(),
822            },
823            ClrInstruction::WithString { opcode, value } => msil::Instruction {
824                opcode: format!("{:?}", opcode).to_lowercase(),
825                operand: Some(format!("\"{}\"", value)),
826                span: (0..0usize).into(),
827            },
828            ClrInstruction::WithLabel { opcode, label } => msil::Instruction {
829                opcode: format!("{:?}", opcode).to_lowercase(),
830                operand: Some(label.clone()),
831                span: (0..0usize).into(),
832            },
833            ClrInstruction::WithSwitch { opcode, labels } => msil::Instruction {
834                opcode: format!("{:?}", opcode).to_lowercase(),
835                operand: Some(labels.join(", ")),
836                span: (0..0usize).into(),
837            },
838        }
839    }
840}
841
842impl ClrConstantPool {
843    /// 创建新的常量池
844    pub fn new() -> Self {
845        Self { strings: HashMap::new(), guids: HashMap::new(), blobs: HashMap::new(), user_strings: HashMap::new() }
846    }
847
848    /// 添加字符串到常量池
849    pub fn add_string(&mut self, s: String) -> u32 {
850        let next_index = self.strings.len() as u32;
851        *self.strings.entry(s).or_insert(next_index)
852    }
853
854    /// 添加 GUID 到常量池
855    pub fn add_guid(&mut self, guid: Vec<u8>) -> u32 {
856        let next_index = self.guids.len() as u32;
857        *self.guids.entry(guid).or_insert(next_index)
858    }
859
860    /// 添加 Blob 到常量池
861    pub fn add_blob(&mut self, blob: Vec<u8>) -> u32 {
862        let next_index = self.blobs.len() as u32;
863        *self.blobs.entry(blob).or_insert(next_index)
864    }
865
866    /// 添加用户字符串到常量池
867    pub fn add_user_string(&mut self, s: String) -> u32 {
868        let next_index = self.user_strings.len() as u32;
869        *self.user_strings.entry(s).or_insert(next_index)
870    }
871}
872
873impl ClrType {
874    /// 创建新的类型
875    pub fn new(name: String, namespace: Option<String>) -> Self {
876        Self {
877            name,
878            namespace,
879            access_flags: ClrAccessFlags::default(),
880            base_type: None,
881            interfaces: Vec::new(),
882            fields: Vec::new(),
883            methods: Vec::new(),
884            properties: Vec::new(),
885            events: Vec::new(),
886            nested_types: Vec::new(),
887            attributes: Vec::new(),
888        }
889    }
890
891    /// 添加方法
892    pub fn add_method(&mut self, method: ClrMethod) {
893        self.methods.push(method);
894    }
895
896    /// 添加字段
897    pub fn add_field(&mut self, field: ClrField) {
898        self.fields.push(field);
899    }
900
901    /// 验证类型定义
902    pub fn validate(&self) -> Result<()> {
903        // 验证类型名称
904        if self.name.is_empty() {
905            return Err(GaiaError::syntax_error("类型名称不能为空".to_string(), SourceLocation::default()));
906        }
907
908        // 验证方法
909        for method in &self.methods {
910            method.validate()?;
911        }
912
913        Ok(())
914    }
915}
916
917impl ClrMethod {
918    /// 创建新的方法
919    pub fn new(name: String, return_type: ClrTypeReference) -> Self {
920        Self {
921            name,
922            return_type,
923            parameters: Vec::new(),
924            access_flags: ClrAccessFlags::default(),
925            impl_flags: ClrMethodImplFlags::default(),
926            instructions: Vec::new(),
927            max_stack: 8,
928            locals: Vec::new(),
929            exception_handlers: Vec::new(),
930            attributes: Vec::new(),
931            is_entry_point: false,
932        }
933    }
934
935    /// 添加指令
936    pub fn add_instruction(&mut self, instruction: ClrInstruction) {
937        self.instructions.push(instruction);
938    }
939
940    /// 添加参数
941    pub fn add_parameter(&mut self, parameter: ClrParameter) {
942        self.parameters.push(parameter);
943    }
944
945    /// 验证方法定义
946    pub fn validate(&self) -> Result<()> {
947        // 验证方法名称
948        if self.name.is_empty() {
949            return Err(GaiaError::syntax_error("方法名称不能为空".to_string(), SourceLocation::default()));
950        }
951
952        Ok(())
953    }
954}
955
956impl Default for ClrVersion {
957    fn default() -> Self {
958        Self { major: 0, minor: 0, build: 0, revision: 0 }
959    }
960}
961
962impl Default for ClrAccessFlags {
963    fn default() -> Self {
964        Self { is_public: false, is_private: true, is_security_transparent: false, is_retargetable: false }
965    }
966}
967
968impl Default for ClrMethodImplFlags {
969    fn default() -> Self {
970        Self {
971            is_managed: true,
972            is_native: false,
973            is_runtime: false,
974            is_inline: false,
975            is_no_inline: false,
976            is_synchronized: false,
977        }
978    }
979}
980
981impl ClrOpcode {
982    /// 将操作码转换为字节
983    pub fn to_byte(&self) -> u8 {
984        match self {
985            ClrOpcode::Nop => 0x00,
986            ClrOpcode::LdcI4M1 => 0x15,
987            ClrOpcode::LdcI40 => 0x16,
988            ClrOpcode::LdcI41 => 0x17,
989            ClrOpcode::LdcI42 => 0x18,
990            ClrOpcode::LdcI43 => 0x19,
991            ClrOpcode::LdcI44 => 0x1A,
992            ClrOpcode::LdcI45 => 0x1B,
993            ClrOpcode::LdcI46 => 0x1C,
994            ClrOpcode::LdcI47 => 0x1D,
995            ClrOpcode::LdcI48 => 0x1E,
996            ClrOpcode::LdcI4S => 0x1F,
997            ClrOpcode::LdcI4 => 0x20,
998            ClrOpcode::LdcI8 => 0x21,
999            ClrOpcode::LdcR4 => 0x22,
1000            ClrOpcode::LdcR8 => 0x23,
1001            ClrOpcode::Ldnull => 0x14,
1002            ClrOpcode::Ldstr => 0x72,
1003            ClrOpcode::Ldarg0 => 0x02,
1004            ClrOpcode::Ldarg1 => 0x03,
1005            ClrOpcode::Ldarg2 => 0x04,
1006            ClrOpcode::Ldarg3 => 0x05,
1007            ClrOpcode::Ldloc0 => 0x06,
1008            ClrOpcode::Ldloc1 => 0x07,
1009            ClrOpcode::Ldloc2 => 0x08,
1010            ClrOpcode::Ldloc3 => 0x09,
1011            ClrOpcode::Stloc0 => 0x0A,
1012            ClrOpcode::Stloc1 => 0x0B,
1013            ClrOpcode::Stloc2 => 0x0C,
1014            ClrOpcode::Stloc3 => 0x0D,
1015            ClrOpcode::Call => 0x28,
1016            ClrOpcode::Callvirt => 0x6F,
1017            ClrOpcode::Ret => 0x2A,
1018            ClrOpcode::Newobj => 0x73,
1019            ClrOpcode::Pop => 0x26,
1020            ClrOpcode::Dup => 0x25,
1021            _ => 0x00, // 默认值,实际使用时需要完善所有操作码
1022        }
1023    }
1024
1025    /// 从字符串解析操作码
1026    pub fn from_str(s: &str) -> Option<Self> {
1027        match s.to_lowercase().as_str() {
1028            "nop" => Some(ClrOpcode::Nop),
1029            "ldc.i4.m1" => Some(ClrOpcode::LdcI4M1),
1030            "ldc.i4.0" => Some(ClrOpcode::LdcI40),
1031            "ldc.i4.1" => Some(ClrOpcode::LdcI41),
1032            "ldc.i4.2" => Some(ClrOpcode::LdcI42),
1033            "ldc.i4.3" => Some(ClrOpcode::LdcI43),
1034            "ldc.i4.4" => Some(ClrOpcode::LdcI44),
1035            "ldc.i4.5" => Some(ClrOpcode::LdcI45),
1036            "ldc.i4.6" => Some(ClrOpcode::LdcI46),
1037            "ldc.i4.7" => Some(ClrOpcode::LdcI47),
1038            "ldc.i4.8" => Some(ClrOpcode::LdcI48),
1039            "ldc.i4.s" => Some(ClrOpcode::LdcI4S),
1040            "ldc.i4" => Some(ClrOpcode::LdcI4),
1041            "ldc.i8" => Some(ClrOpcode::LdcI8),
1042            "ldc.r4" => Some(ClrOpcode::LdcR4),
1043            "ldc.r8" => Some(ClrOpcode::LdcR8),
1044            "ldnull" => Some(ClrOpcode::Ldnull),
1045            "ldstr" => Some(ClrOpcode::Ldstr),
1046            "ldarg.0" => Some(ClrOpcode::Ldarg0),
1047            "ldarg.1" => Some(ClrOpcode::Ldarg1),
1048            "ldarg.2" => Some(ClrOpcode::Ldarg2),
1049            "ldarg.3" => Some(ClrOpcode::Ldarg3),
1050            "ldloc.0" => Some(ClrOpcode::Ldloc0),
1051            "ldloc.1" => Some(ClrOpcode::Ldloc1),
1052            "ldloc.2" => Some(ClrOpcode::Ldloc2),
1053            "ldloc.3" => Some(ClrOpcode::Ldloc3),
1054            "stloc.0" => Some(ClrOpcode::Stloc0),
1055            "stloc.1" => Some(ClrOpcode::Stloc1),
1056            "stloc.2" => Some(ClrOpcode::Stloc2),
1057            "stloc.3" => Some(ClrOpcode::Stloc3),
1058            "call" => Some(ClrOpcode::Call),
1059            "callvirt" => Some(ClrOpcode::Callvirt),
1060            "ret" => Some(ClrOpcode::Ret),
1061            "newobj" => Some(ClrOpcode::Newobj),
1062            "pop" => Some(ClrOpcode::Pop),
1063            "dup" => Some(ClrOpcode::Dup),
1064            _ => None,
1065        }
1066    }
1067}
1068
1069// 为了兼容性,重新导出一些类型
1070// pub use ClrProgram as DotNetAssemblyInfo; // 注释掉以避免重复定义
1071
1072/// 旧版本的 CLR 头结构,用于向后兼容
1073#[derive(Copy, Debug, Clone)]
1074pub struct ClrHeader {
1075    /// 头的总大小(字节)
1076    pub cb: u32,
1077    /// CLR 运行时主版本号
1078    pub major_runtime_version: u16,
1079    /// CLR 运行时次版本号
1080    pub minor_runtime_version: u16,
1081    /// 元数据的相对虚拟地址
1082    pub metadata_rva: u32,
1083    /// 元数据的大小(字节)
1084    pub metadata_size: u32,
1085    /// 程序集的标志位,如是否为纯 IL 代码等
1086    pub flags: u32,
1087}
1088
1089/// 旧版本的元数据头结构,用于向后兼容
1090#[derive(Debug, Clone)]
1091pub struct MetadataHeader {
1092    /// 魔数,通常为 0x424A5342 (BSJB)
1093    pub signature: u32,
1094    /// 元数据格式主版本
1095    pub major_version: u16,
1096    /// 元数据格式次版本
1097    pub minor_version: u16,
1098    /// 保留字段,通常为 0
1099    pub reserved: u32,
1100    /// 运行时版本字符串的长度
1101    pub version_length: u32,
1102    /// 运行时版本字符串的内容
1103    pub version_string: String,
1104    /// 元数据标志位
1105    pub flags: u16,
1106    /// 元数据流的数量
1107    pub streams: u16,
1108}
1109
1110/// 旧版本的流头结构,用于向后兼容
1111#[derive(Debug, Clone)]
1112pub struct StreamHeader {
1113    /// 该流在元数据中的偏移量
1114    pub offset: u32,
1115    /// 流的大小(字节)
1116    pub size: u32,
1117    /// 流的名称,如 "#Strings"、"#US"、"#GUID"、"#Blob" 等
1118    pub name: String,
1119}
1120
1121/// 旧版本的 .NET 程序集信息,用于向后兼容
1122#[derive(Debug, Clone)]
1123pub struct DotNetAssemblyInfo {
1124    /// 程序集名称
1125    pub name: String,
1126    /// 版本号,格式为 major.minor.build.revision
1127    pub version: String,
1128    /// 文化区域信息,如 "zh-CN",null 表示中性文化
1129    pub culture: Option<String>,
1130    /// 公钥标记,用于强名称验证
1131    pub public_key_token: Option<String>,
1132    /// .NET 运行时版本,如 "v4.0.30319"
1133    pub runtime_version: Option<String>,
1134}