Skip to content

Commit

Permalink
Do not handle inline assembly with "intel" flag as AT&T syntax (#14264
Browse files Browse the repository at this point in the history
)

Co-authored-by: Johannes Müller <[email protected]>
  • Loading branch information
HertzDevil and straight-shoota authored Jan 29, 2024
1 parent 0f7c4e0 commit 4b8f5c5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 11 deletions.
8 changes: 8 additions & 0 deletions spec/compiler/codegen/asm_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,13 @@ describe "Code gen: asm" do
c
)).to_i.should eq(42)
end

it "codegens with intel dialect" do
run(<<-CRYSTAL).to_i.should eq(1234)
dst = uninitialized Int32
asm("mov dword ptr [$0], 1234" :: "r"(pointerof(dst)) :: "intel")
dst
CRYSTAL
end
{% end %}
end
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/asm.cr
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Crystal::CodeGenVisitor
fun_type = LLVM::Type.function(input_types, output_type)
constraints = constraints.to_s

value = fun_type.inline_asm(node.text, constraints, node.volatile?, node.alignstack?, node.can_throw?)
value = fun_type.inline_asm(node.text, constraints, node.volatile?, node.alignstack?, node.can_throw?, node.dialect)
value = LLVM::Function.from_value(value)
asm_value = call LLVMTypedFunction.new(fun_type, value), input_values

Expand Down
6 changes: 6 additions & 0 deletions src/compiler/crystal/codegen/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,10 @@ module Crystal
found_extern
end
end

class Asm
def dialect : LLVM::InlineAsmDialect
intel? ? LLVM::InlineAsmDialect::Intel : LLVM::InlineAsmDialect::ATT
end
end
end
5 changes: 5 additions & 0 deletions src/llvm/enums.cr
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ module LLVM
LittleEndian = 1 << 28
end

enum InlineAsmDialect
ATT
Intel
end

struct Value
enum Kind
Argument
Expand Down
9 changes: 2 additions & 7 deletions src/llvm/lib_llvm/core.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ lib LibLLVM
# NOTE: the following C enums usually have different values from their C++
# counterparts (e.g. `LLVMModuleFlagBehavior` v.s. `LLVM::Module::ModFlagBehavior`)

enum InlineAsmDialect
ATT
Intel
end

enum ModuleFlagBehavior
Warning = 1
end
Expand Down Expand Up @@ -39,9 +34,9 @@ lib LibLLVM
fun print_module_to_file = LLVMPrintModuleToFile(m : ModuleRef, filename : Char*, error_message : Char**) : Bool
fun print_module_to_string = LLVMPrintModuleToString(m : ModuleRef) : Char*
{% if !LibLLVM::IS_LT_130 %}
fun get_inline_asm = LLVMGetInlineAsm(ty : TypeRef, asm_string : Char*, asm_string_size : SizeT, constraints : Char*, constraints_size : SizeT, has_side_effects : Bool, is_align_stack : Bool, dialect : InlineAsmDialect, can_throw : Bool) : ValueRef
fun get_inline_asm = LLVMGetInlineAsm(ty : TypeRef, asm_string : Char*, asm_string_size : SizeT, constraints : Char*, constraints_size : SizeT, has_side_effects : Bool, is_align_stack : Bool, dialect : LLVM::InlineAsmDialect, can_throw : Bool) : ValueRef
{% else %}
fun get_inline_asm = LLVMGetInlineAsm(t : TypeRef, asm_string : Char*, asm_string_size : SizeT, constraints : Char*, constraints_size : SizeT, has_side_effects : Bool, is_align_stack : Bool, dialect : InlineAsmDialect) : ValueRef
fun get_inline_asm = LLVMGetInlineAsm(t : TypeRef, asm_string : Char*, asm_string_size : SizeT, constraints : Char*, constraints_size : SizeT, has_side_effects : Bool, is_align_stack : Bool, dialect : LLVM::InlineAsmDialect) : ValueRef
{% end %}
fun get_module_context = LLVMGetModuleContext(m : ModuleRef) : ContextRef

Expand Down
6 changes: 3 additions & 3 deletions src/llvm/type.cr
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ struct LLVM::Type
Value.new LibLLVM.const_array(self, (values.to_unsafe.as(LibLLVM::ValueRef*)), values.size)
end

def inline_asm(asm_string, constraints, has_side_effects = false, is_align_stack = false, can_throw = false)
def inline_asm(asm_string, constraints, has_side_effects = false, is_align_stack = false, can_throw = false, dialect : InlineAsmDialect = InlineAsmDialect::ATT)
value =
{% if LibLLVM::IS_LT_130 %}
LibLLVM.get_inline_asm(
Expand All @@ -184,7 +184,7 @@ struct LLVM::Type
constraints.size,
(has_side_effects ? 1 : 0),
(is_align_stack ? 1 : 0),
LibLLVM::InlineAsmDialect::ATT
dialect,
)
{% else %}
LibLLVM.get_inline_asm(
Expand All @@ -195,7 +195,7 @@ struct LLVM::Type
constraints.size,
(has_side_effects ? 1 : 0),
(is_align_stack ? 1 : 0),
LibLLVM::InlineAsmDialect::ATT,
dialect,
(can_throw ? 1 : 0)
)
{% end %}
Expand Down

0 comments on commit 4b8f5c5

Please sign in to comment.