clr_assembler/program/
mod.rs

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