Somewhat academic question, but: How do value types like Int actually work?
有点学术问题,但是:像Int这样的值类型是如何工作的呢?
I've used Reflector on mscorlib to find out how System.Int32 is implemented, and it's just a Struct that inherits from System.ValueType. I was looking for something among the lines of an Array of Bits holding the value, but I only found a field that's declared int - which means it's a circular reference?
我已经在mscorlib上用Reflector找到系统。Int32实现了,它只是继承自System.ValueType的结构体。我在一个包含值的位数组中寻找一些东西,但我只找到了一个声明为int的字段——这意味着它是一个循环引用?
I mean, I can write "int i = 14;", but the number 14 needs to be stored somewhere somehow, but I couldn't find the "Array of 32-Bits" or a Pointer or something.
我的意思是,我可以写"int I = 14 ",但数字14需要以某种方式存储在某处,但我找不到“32位数组”或指针之类的东西。
Is this some magic that the compiler does, and are these magic types part of the specification? (Similar to how System.Attribute or System.Exception are "special" types)
这是编译器的魔法吗?这些魔法类型是规范的一部分吗?(类似于系统。属性或系统。例外是“特殊”类型)
Edit: If I declare my own struct, I add fields to it. Those fields are of a built-in type, for example int. So the CLR knows that I hold an int. But how does it know that an int is 32-Bits, signed? Is it simply that the Specification specifies certain base types and therefore makes them "magic", or is there a technical mechanism? Hypothetical example: If I would want to declare an Int36, that is an Integer with 36 Bits, could I create a type that works exactly like an Int32 (apart from the 4 extra bits ofc) by specifying "Okay, set aside 36 bits", or are the built-in primitives set in stone and I would have to somehow work around this (i.e. by using an Int64 and code that only sets the last 36 Bits)?
编辑:如果我声明我自己的结构,我添加字段。这些字段都是内置类型,例如int.所以CLR知道我持有int.但是它怎么知道int是32位的,有符号的?是规范简单地指定了某些基类型,从而使它们具有“魔力”,还是有技术机制?假想的例子:如果我想声明一个Int36,与36个整数位,我可以创建一个类型作品就像一个Int32(除了额外的4位ofc)通过指定“好,留出36位”,还是内置的原语在石头上,我就会想办法解决这个(即通过使用Int64和代码,只有集最后36位)?
As said, all very academic and hypothetical, but I always wondered about that.
就像我说的,都是学术的和假设的,但我一直很好奇。
2 个解决方案
#1
3
Certain primitive types like integers are part of the CLI specification. For example, there are specific IL instructions like ldc.i4 for loading values of these types, and IL instructions such as add have specific knowledge of these types. (Your example of int i = 14
would get compiled to ldc.i4 14
, with the 14 represented internally as part of the opcode within the compiled MSIL.)
某些基本类型(如整数)是CLI规范的一部分。例如,有特定的IL指令,比如ldc。i4用于加载这些类型的值,IL指令(如add)具有这些类型的特定知识。(您的int i = 14示例将编译到ldc。i4 14,在编译后的MSIL中有14个内部作为opcode的一部分。
For more info, see Partition IIa of the CLI spec, Section 7.2, "Built-in Types." (Can't find a link to the particular section, sorry.) The built-in types are: bool, char, object, string, float32, float64, int[8|16|32|64], unsigned int[8|1632|64], native int (IntPtr), native unsigned int, and typedref. The spec notes that they "have corresponding value types defined in the Base Class Library" which makes me think that Int32 is actually kind of a metadata wrapper around the "real" int32, which lives down at the VES level.
有关更多信息,请参阅CLI spec的分区IIa,第7.2节“内置类型”。(找不到链接到特定的部分,抱歉。)内置的类型有:bool、char、object、string、float32、float64、int[8|16|32|64]、unsigned int[8|1632|64]、本机int (IntPtr)、本机unsigned int和typedref。规范指出,它们“在基类库中定义了相应的值类型”,这使我认为Int32实际上是“真实”Int32的元数据包装器,它位于VES级别。
Other value types, like System.Decimal, System.Drawing.Point or any structs you define in your own code, are non-magical.
其他值类型,比如System。十进制、System.Drawing。点或您在自己的代码中定义的任何结构,都是非魔法的。
#2
1
Int32 is a compiler intrinsic type, meaning the compiler has special logic to deal with it. The actual implementation of Int32 in the framework is meaningless.
Int32是一个编译器内部类型,这意味着编译器有特殊的逻辑来处理它。框架中Int32的实际实现是没有意义的。
It's worth noting that Int16, Int32, UInt16, UInt32, Single, Double, etc. roughly correspond to the types which are native to the x86 instruction set (and others). Creating a Int36 type would likewise require using a native type as a foundation in pure assembly code.
值得注意的是,Int16、Int32、UInt16、UInt32、Single、Double等大致对应于x86指令集(以及其他)的原生类型。创建Int36类型同样需要使用本机类型作为纯汇编代码的基础。
#1
3
Certain primitive types like integers are part of the CLI specification. For example, there are specific IL instructions like ldc.i4 for loading values of these types, and IL instructions such as add have specific knowledge of these types. (Your example of int i = 14
would get compiled to ldc.i4 14
, with the 14 represented internally as part of the opcode within the compiled MSIL.)
某些基本类型(如整数)是CLI规范的一部分。例如,有特定的IL指令,比如ldc。i4用于加载这些类型的值,IL指令(如add)具有这些类型的特定知识。(您的int i = 14示例将编译到ldc。i4 14,在编译后的MSIL中有14个内部作为opcode的一部分。
For more info, see Partition IIa of the CLI spec, Section 7.2, "Built-in Types." (Can't find a link to the particular section, sorry.) The built-in types are: bool, char, object, string, float32, float64, int[8|16|32|64], unsigned int[8|1632|64], native int (IntPtr), native unsigned int, and typedref. The spec notes that they "have corresponding value types defined in the Base Class Library" which makes me think that Int32 is actually kind of a metadata wrapper around the "real" int32, which lives down at the VES level.
有关更多信息,请参阅CLI spec的分区IIa,第7.2节“内置类型”。(找不到链接到特定的部分,抱歉。)内置的类型有:bool、char、object、string、float32、float64、int[8|16|32|64]、unsigned int[8|1632|64]、本机int (IntPtr)、本机unsigned int和typedref。规范指出,它们“在基类库中定义了相应的值类型”,这使我认为Int32实际上是“真实”Int32的元数据包装器,它位于VES级别。
Other value types, like System.Decimal, System.Drawing.Point or any structs you define in your own code, are non-magical.
其他值类型,比如System。十进制、System.Drawing。点或您在自己的代码中定义的任何结构,都是非魔法的。
#2
1
Int32 is a compiler intrinsic type, meaning the compiler has special logic to deal with it. The actual implementation of Int32 in the framework is meaningless.
Int32是一个编译器内部类型,这意味着编译器有特殊的逻辑来处理它。框架中Int32的实际实现是没有意义的。
It's worth noting that Int16, Int32, UInt16, UInt32, Single, Double, etc. roughly correspond to the types which are native to the x86 instruction set (and others). Creating a Int36 type would likewise require using a native type as a foundation in pure assembly code.
值得注意的是,Int16、Int32、UInt16、UInt32、Single、Double等大致对应于x86指令集(以及其他)的原生类型。创建Int36类型同样需要使用本机类型作为纯汇编代码的基础。