Crystal-lang 中的 'Primitive' 属性是什么?
What is 'Primitive' attribute in Crystal-lang?
在阅读 Crystal-lang 内部代码时,我发现了一些片段,例如:
@[Primitive(:some_name)]
def some_method
end
谁能给我解释一下 Primitive
属性的作用及其工作原理(例如告诉编译器使用 LLVM 函数)?
这是无法用纯粹的 Crystal 表达的功能。任何时候出现对此类方法的调用,都需要由编译器直接用低级指令 (LLVM IR) 替换,而不是由实际函数支持。
这是必需的,因为一些操作已经处于最低级别。例如,如何实现两个数的相加?呃,a+b
?等等,但这就是我们正在努力实现的。因此,除了诉诸 if a==1 && b==1 ...
之类的荒谬事情之外,这需要推迟到 CPU(通过 LLVM IR)。
文件primitives.cr contains only empty shells of such methods, and also the documentation for them, so they look like a normal part of the standard library. They are marked by an annotation for the compiler to know when to replace them. And the real action happens in compiler/crystal/codegen/primitives.cr:每个注释名称都有一个case
分支,这导致生成适当代码的代码。
为了完整性,这里摘录自 primitives.cr:
Methods defined here are primitives because they either:
- can't be expressed in Crystal (need to be expressed in LLVM). For example unary
and binary math operators fall into this category.
- should always be inlined with an LLVM instruction for performance reasons, even
in non-release builds. An example of this is
Char#ord
, which could be implemented
in Crystal by assigning self
to a variable and casting a pointer to it to Int32
,
and then reading back the value.
旁注:
这不是编译器实现不能是普通 stdlib 方法的方法的唯一方法。对于 obj.<a href="https://crystal-lang.org/docs/syntax_and_semantics/is_a.html" rel="noreferrer">is_a?</a>
, on a syntactic level. This is an even "stronger" ability, and the pretense of it being a method is dropped (you won't find is_a?
in Object's documentation). This kind of construct is tied deeply into the compiler and can affect type information.
这样的伪方法也会发生这种情况
在阅读 Crystal-lang 内部代码时,我发现了一些片段,例如:
@[Primitive(:some_name)]
def some_method
end
谁能给我解释一下 Primitive
属性的作用及其工作原理(例如告诉编译器使用 LLVM 函数)?
这是无法用纯粹的 Crystal 表达的功能。任何时候出现对此类方法的调用,都需要由编译器直接用低级指令 (LLVM IR) 替换,而不是由实际函数支持。
这是必需的,因为一些操作已经处于最低级别。例如,如何实现两个数的相加?呃,a+b
?等等,但这就是我们正在努力实现的。因此,除了诉诸 if a==1 && b==1 ...
之类的荒谬事情之外,这需要推迟到 CPU(通过 LLVM IR)。
文件primitives.cr contains only empty shells of such methods, and also the documentation for them, so they look like a normal part of the standard library. They are marked by an annotation for the compiler to know when to replace them. And the real action happens in compiler/crystal/codegen/primitives.cr:每个注释名称都有一个case
分支,这导致生成适当代码的代码。
为了完整性,这里摘录自 primitives.cr:
Methods defined here are primitives because they either:
- can't be expressed in Crystal (need to be expressed in LLVM). For example unary and binary math operators fall into this category.
- should always be inlined with an LLVM instruction for performance reasons, even in non-release builds. An example of this is
Char#ord
, which could be implemented in Crystal by assigningself
to a variable and casting a pointer to it toInt32
, and then reading back the value.
旁注:
这不是编译器实现不能是普通 stdlib 方法的方法的唯一方法。对于 obj.<a href="https://crystal-lang.org/docs/syntax_and_semantics/is_a.html" rel="noreferrer">is_a?</a>
, on a syntactic level. This is an even "stronger" ability, and the pretense of it being a method is dropped (you won't find is_a?
in Object's documentation). This kind of construct is tied deeply into the compiler and can affect type information.