diff --git a/llvm/test/tools/llvm-ml/alias.test b/llvm/test/tools/llvm-ml/alias.asm rename from llvm/test/tools/llvm-ml/alias.test rename to llvm/test/tools/llvm-ml/alias.asm --- a/llvm/test/tools/llvm-ml/alias.test +++ b/llvm/test/tools/llvm-ml/alias.asm @@ -1,4 +1,4 @@ -; RUN: llvm-ml -filetype=obj %s | llvm-readobj --syms - | FileCheck %s +; RUN: llvm-ml %s /Fo - | llvm-readobj --syms - | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/alias_errors.test b/llvm/test/tools/llvm-ml/alias_errors.asm rename from llvm/test/tools/llvm-ml/alias_errors.test rename to llvm/test/tools/llvm-ml/alias_errors.asm --- a/llvm/test/tools/llvm-ml/alias_errors.test +++ b/llvm/test/tools/llvm-ml/alias_errors.asm @@ -1,4 +1,4 @@ -; RUN: not llvm-ml -filetype=asm %s 2>&1 | FileCheck %s +; RUN: not llvm-ml -filetype=s %s /Fo /dev/null 2>&1 | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/basic.test b/llvm/test/tools/llvm-ml/basic.test --- a/llvm/test/tools/llvm-ml/basic.test +++ b/llvm/test/tools/llvm-ml/basic.test @@ -1,3 +1,3 @@ -# RUN: not llvm-ml %t.blah -o /dev/null 2>&1 | FileCheck --check-prefix=ENOENT %s +# RUN: not llvm-ml %t.blah.asm /Fo /dev/null 2>&1 | FileCheck --check-prefix=ENOENT %s -# ENOENT: {{.*}}.blah: {{[Nn]}}o such file or directory +# ENOENT: {{.*}}.blah.asm: {{[Nn]}}o such file or directory diff --git a/llvm/test/tools/llvm-ml/basic_data.test b/llvm/test/tools/llvm-ml/basic_data.asm rename from llvm/test/tools/llvm-ml/basic_data.test rename to llvm/test/tools/llvm-ml/basic_data.asm --- a/llvm/test/tools/llvm-ml/basic_data.test +++ b/llvm/test/tools/llvm-ml/basic_data.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .data BYTE 2, 4, 6, 8 diff --git a/llvm/test/tools/llvm-ml/builtin_types.test b/llvm/test/tools/llvm-ml/builtin_types.asm rename from llvm/test/tools/llvm-ml/builtin_types.test rename to llvm/test/tools/llvm-ml/builtin_types.asm --- a/llvm/test/tools/llvm-ml/builtin_types.test +++ b/llvm/test/tools/llvm-ml/builtin_types.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/dot_operator.test b/llvm/test/tools/llvm-ml/dot_operator.asm rename from llvm/test/tools/llvm-ml/dot_operator.test rename to llvm/test/tools/llvm-ml/dot_operator.asm --- a/llvm/test/tools/llvm-ml/dot_operator.test +++ b/llvm/test/tools/llvm-ml/dot_operator.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -m64 -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/expansion.test b/llvm/test/tools/llvm-ml/expansion.asm rename from llvm/test/tools/llvm-ml/expansion.test rename to llvm/test/tools/llvm-ml/expansion.asm --- a/llvm/test/tools/llvm-ml/expansion.test +++ b/llvm/test/tools/llvm-ml/expansion.asm @@ -1,4 +1,4 @@ -; RUN: llvm-ml -filetype=asm %s 2>&1 | FileCheck %s +; RUN: llvm-ml -filetype=s %s /Fo /dev/null 2>&1 | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/feat00.test b/llvm/test/tools/llvm-ml/feat00.asm rename from llvm/test/tools/llvm-ml/feat00.test rename to llvm/test/tools/llvm-ml/feat00.asm --- a/llvm/test/tools/llvm-ml/feat00.test +++ b/llvm/test/tools/llvm-ml/feat00.asm @@ -1,8 +1,8 @@ -; RUN: llvm-ml -m32 -filetype=obj %s | llvm-readobj --syms - | FileCheck %s --check-prefix=CHECK-OBJ --check-prefix=CHECK-OBJ-NOSAFESEH -; RUN: llvm-ml -m64 -filetype=obj %s | llvm-readobj --syms - | FileCheck %s --check-prefix=CHECK-OBJ --check-prefix=CHECK-OBJ-NOSAFESEH +; RUN: llvm-ml -m32 %s /Fo - | llvm-readobj --syms - | FileCheck %s --check-prefix=CHECK-OBJ --check-prefix=CHECK-OBJ-NOSAFESEH +; RUN: llvm-ml -m64 %s /Fo - | llvm-readobj --syms - | FileCheck %s --check-prefix=CHECK-OBJ --check-prefix=CHECK-OBJ-NOSAFESEH -; RUN: llvm-ml -m32 -safeseh -filetype=obj %s | llvm-readobj --syms - | FileCheck %s --check-prefix=CHECK-OBJ --check-prefix=CHECK-OBJ-SAFESEH -; RUN: llvm-ml -m64 -safeseh -filetype=obj %s -o %t.obj 2>&1 | FileCheck %s --check-prefix=CHECK-SAFESEH64 +; RUN: llvm-ml -m32 -safeseh %s /Fo - | llvm-readobj --syms - | FileCheck %s --check-prefix=CHECK-OBJ --check-prefix=CHECK-OBJ-SAFESEH +; RUN: llvm-ml -m64 -safeseh %s /Fo %t.obj 2>&1 | FileCheck %s --check-prefix=CHECK-SAFESEH64 ; RUN: llvm-readobj --syms %t.obj | FileCheck %s --check-prefix=CHECK-OBJ --check-prefix=CHECK-OBJ-NOSAFESEH ; CHECK-SAFESEH64: warning: /safeseh applies only to 32-bit X86 platforms; ignoring. diff --git a/llvm/test/tools/llvm-ml/feat00_override.test b/llvm/test/tools/llvm-ml/feat00_override.asm rename from llvm/test/tools/llvm-ml/feat00_override.test rename to llvm/test/tools/llvm-ml/feat00_override.asm --- a/llvm/test/tools/llvm-ml/feat00_override.test +++ b/llvm/test/tools/llvm-ml/feat00_override.asm @@ -1,6 +1,6 @@ -; RUN: llvm-ml -m32 -filetype=obj %s | llvm-readobj --syms - | FileCheck %s -; RUN: llvm-ml -m64 -filetype=obj %s | llvm-readobj --syms - | FileCheck %s -; RUN: llvm-ml -m32 -safeseh -filetype=obj %s | llvm-readobj --syms - | FileCheck %s +; RUN: llvm-ml -m32 %s /Fo - | llvm-readobj --syms - | FileCheck %s +; RUN: llvm-ml -m64 %s /Fo - | llvm-readobj --syms - | FileCheck %s +; RUN: llvm-ml -m32 -safeseh %s /Fo - | llvm-readobj --syms - | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/line_continuations.test b/llvm/test/tools/llvm-ml/line_continuations.asm rename from llvm/test/tools/llvm-ml/line_continuations.test rename to llvm/test/tools/llvm-ml/line_continuations.asm --- a/llvm/test/tools/llvm-ml/line_continuations.test +++ b/llvm/test/tools/llvm-ml/line_continuations.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/lit.local.cfg b/llvm/test/tools/llvm-ml/lit.local.cfg --- a/llvm/test/tools/llvm-ml/lit.local.cfg +++ b/llvm/test/tools/llvm-ml/lit.local.cfg @@ -1,3 +1,5 @@ if not ('X86' in config.root.targets): # We need support for X86. config.unsupported = True + +config.suffixes.add('.asm') diff --git a/llvm/test/tools/llvm-ml/macro.test b/llvm/test/tools/llvm-ml/macro.asm rename from llvm/test/tools/llvm-ml/macro.test rename to llvm/test/tools/llvm-ml/macro.asm --- a/llvm/test/tools/llvm-ml/macro.test +++ b/llvm/test/tools/llvm-ml/macro.asm @@ -1,4 +1,4 @@ -; RUN: llvm-ml -m64 -filetype=asm %s | FileCheck %s +; RUN: llvm-ml -m64 -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/macro_errors.test b/llvm/test/tools/llvm-ml/macro_errors.asm rename from llvm/test/tools/llvm-ml/macro_errors.test rename to llvm/test/tools/llvm-ml/macro_errors.asm --- a/llvm/test/tools/llvm-ml/macro_errors.test +++ b/llvm/test/tools/llvm-ml/macro_errors.asm @@ -1,4 +1,4 @@ -; RUN: not llvm-ml -filetype=asm %s 2>&1 | FileCheck %s --implicit-check-not=error: +; RUN: not llvm-ml -filetype=s %s /Fo - 2>&1 | FileCheck %s --implicit-check-not=error: .code diff --git a/llvm/test/tools/llvm-ml/macro_function.test b/llvm/test/tools/llvm-ml/macro_function.asm rename from llvm/test/tools/llvm-ml/macro_function.test rename to llvm/test/tools/llvm-ml/macro_function.asm --- a/llvm/test/tools/llvm-ml/macro_function.test +++ b/llvm/test/tools/llvm-ml/macro_function.asm @@ -1,4 +1,4 @@ -; RUN: llvm-ml -filetype=asm %s | FileCheck %s +; RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/named_operators.test b/llvm/test/tools/llvm-ml/named_operators.asm rename from llvm/test/tools/llvm-ml/named_operators.test rename to llvm/test/tools/llvm-ml/named_operators.asm --- a/llvm/test/tools/llvm-ml/named_operators.test +++ b/llvm/test/tools/llvm-ml/named_operators.asm @@ -1,4 +1,4 @@ -; RUN: llvm-ml -filetype=asm %s | FileCheck %s +; RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/proc.asm b/llvm/test/tools/llvm-ml/proc.asm new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-ml/proc.asm @@ -0,0 +1,13 @@ +# RUN: llvm-ml -m32 -filetype=s %s /Fo - | FileCheck %s +# RUN: llvm-ml -m64 -filetype=s %s /Fo - | FileCheck %s + +.code + +t1 PROC + ret +t1 ENDP + +; CHECK: t1: +; CHECK: ret + +END diff --git a/llvm/test/tools/llvm-ml/proc.test b/llvm/test/tools/llvm-ml/proc.test deleted file mode 100644 --- a/llvm/test/tools/llvm-ml/proc.test +++ /dev/null @@ -1,13 +0,0 @@ -# RUN: llvm-ml -m32 -filetype=asm %s | FileCheck %s -# RUN: llvm-ml -m64 -filetype=asm %s | FileCheck %s - -.code - -t1 PROC - ret -t1 ENDP - -; CHECK: t1: -; CHECK: ret - -END diff --git a/llvm/test/tools/llvm-ml/proc_frame.test b/llvm/test/tools/llvm-ml/proc_frame.asm rename from llvm/test/tools/llvm-ml/proc_frame.test rename to llvm/test/tools/llvm-ml/proc_frame.asm --- a/llvm/test/tools/llvm-ml/proc_frame.test +++ b/llvm/test/tools/llvm-ml/proc_frame.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -m64 -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -m64 -filetype=s %s /Fo - | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/radix.test b/llvm/test/tools/llvm-ml/radix.asm rename from llvm/test/tools/llvm-ml/radix.test rename to llvm/test/tools/llvm-ml/radix.asm --- a/llvm/test/tools/llvm-ml/radix.test +++ b/llvm/test/tools/llvm-ml/radix.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .code diff --git a/llvm/test/tools/llvm-ml/radix_errors.test b/llvm/test/tools/llvm-ml/radix_errors.asm rename from llvm/test/tools/llvm-ml/radix_errors.test rename to llvm/test/tools/llvm-ml/radix_errors.asm --- a/llvm/test/tools/llvm-ml/radix_errors.test +++ b/llvm/test/tools/llvm-ml/radix_errors.asm @@ -1,4 +1,4 @@ -; RUN: not llvm-ml -filetype=asm %s 2>&1 | FileCheck %s --implicit-check-not=error: +; RUN: not llvm-ml -filetype=s %s /Fo /dev/null 2>&1 | FileCheck %s --implicit-check-not=error: .code diff --git a/llvm/test/tools/llvm-ml/repeat_directives.test b/llvm/test/tools/llvm-ml/repeat_directives.asm rename from llvm/test/tools/llvm-ml/repeat_directives.test rename to llvm/test/tools/llvm-ml/repeat_directives.asm --- a/llvm/test/tools/llvm-ml/repeat_directives.test +++ b/llvm/test/tools/llvm-ml/repeat_directives.asm @@ -1,4 +1,4 @@ -; RUN: llvm-ml -m64 -filetype=asm %s | FileCheck %s +; RUN: llvm-ml -m64 -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/rip-relative-addressing.asm b/llvm/test/tools/llvm-ml/rip-relative-addressing.asm new file mode 100644 --- /dev/null +++ b/llvm/test/tools/llvm-ml/rip-relative-addressing.asm @@ -0,0 +1,8 @@ +# RUN: llvm-ml -m32 -filetype=s %s /Fo - | FileCheck %s --check-prefix=CHECK-32 +# RUN: llvm-ml -m64 -filetype=s %s /Fo - | FileCheck %s --check-prefix=CHECK-64 + +.code +mov eax, [4] +; CHECK-32: mov eax, dword ptr [4] +; CHECK-64: mov eax, dword ptr [rip + 4] +END \ No newline at end of file diff --git a/llvm/test/tools/llvm-ml/rip-relative-addressing.test b/llvm/test/tools/llvm-ml/rip-relative-addressing.test deleted file mode 100644 --- a/llvm/test/tools/llvm-ml/rip-relative-addressing.test +++ /dev/null @@ -1,8 +0,0 @@ -# RUN: llvm-ml -m32 -filetype=asm %s | FileCheck %s --check-prefix=CHECK-32 -# RUN: llvm-ml -m64 -filetype=asm %s | FileCheck %s --check-prefix=CHECK-64 - -.code -mov eax, [4] -; CHECK-32: mov eax, dword ptr [4] -; CHECK-64: mov eax, dword ptr [rip + 4] -END \ No newline at end of file diff --git a/llvm/test/tools/llvm-ml/run.test b/llvm/test/tools/llvm-ml/run.asm rename from llvm/test/tools/llvm-ml/run.test rename to llvm/test/tools/llvm-ml/run.asm diff --git a/llvm/test/tools/llvm-ml/size_inference.test b/llvm/test/tools/llvm-ml/size_inference.asm rename from llvm/test/tools/llvm-ml/size_inference.test rename to llvm/test/tools/llvm-ml/size_inference.asm --- a/llvm/test/tools/llvm-ml/size_inference.test +++ b/llvm/test/tools/llvm-ml/size_inference.asm @@ -1,4 +1,4 @@ -; RUN: not llvm-ml -filetype=asm %s 2>&1 | FileCheck %s --dump-input=always +; RUN: not llvm-ml -filetype=s %s /Fo /dev/null 2>&1 | FileCheck %s --dump-input=always .data diff --git a/llvm/test/tools/llvm-ml/strings.test b/llvm/test/tools/llvm-ml/strings.asm rename from llvm/test/tools/llvm-ml/strings.test rename to llvm/test/tools/llvm-ml/strings.asm --- a/llvm/test/tools/llvm-ml/strings.test +++ b/llvm/test/tools/llvm-ml/strings.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +; RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/struct.test b/llvm/test/tools/llvm-ml/struct.asm rename from llvm/test/tools/llvm-ml/struct.test rename to llvm/test/tools/llvm-ml/struct.asm --- a/llvm/test/tools/llvm-ml/struct.test +++ b/llvm/test/tools/llvm-ml/struct.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -m64 -filetype=s %s /Fo - | FileCheck %s .data BAZ STRUCT diff --git a/llvm/test/tools/llvm-ml/struct_alignment.test b/llvm/test/tools/llvm-ml/struct_alignment.asm rename from llvm/test/tools/llvm-ml/struct_alignment.test rename to llvm/test/tools/llvm-ml/struct_alignment.asm --- a/llvm/test/tools/llvm-ml/struct_alignment.test +++ b/llvm/test/tools/llvm-ml/struct_alignment.asm @@ -1,4 +1,4 @@ -; RUN: llvm-ml -filetype=asm %s | FileCheck %s +; RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/struct_errors.test b/llvm/test/tools/llvm-ml/struct_errors.asm rename from llvm/test/tools/llvm-ml/struct_errors.test rename to llvm/test/tools/llvm-ml/struct_errors.asm --- a/llvm/test/tools/llvm-ml/struct_errors.test +++ b/llvm/test/tools/llvm-ml/struct_errors.asm @@ -1,4 +1,4 @@ -# RUN: not llvm-ml -filetype=asm %s 2>&1 | FileCheck %s --dump-input=always +# RUN: not llvm-ml -filetype=s %s /Fo /dev/null 2>&1 | FileCheck %s --dump-input=always .data int_test STRUCT diff --git a/llvm/test/tools/llvm-ml/type_operators.test b/llvm/test/tools/llvm-ml/type_operators.asm rename from llvm/test/tools/llvm-ml/type_operators.test rename to llvm/test/tools/llvm-ml/type_operators.asm --- a/llvm/test/tools/llvm-ml/type_operators.test +++ b/llvm/test/tools/llvm-ml/type_operators.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .data diff --git a/llvm/test/tools/llvm-ml/variable.test b/llvm/test/tools/llvm-ml/variable.asm rename from llvm/test/tools/llvm-ml/variable.test rename to llvm/test/tools/llvm-ml/variable.asm --- a/llvm/test/tools/llvm-ml/variable.test +++ b/llvm/test/tools/llvm-ml/variable.asm @@ -1,4 +1,4 @@ -# RUN: llvm-ml -filetype=asm %s | FileCheck %s +# RUN: llvm-ml -filetype=s %s /Fo - | FileCheck %s .data t1_value equ 1 or 2 diff --git a/llvm/tools/llvm-ml/CMakeLists.txt b/llvm/tools/llvm-ml/CMakeLists.txt --- a/llvm/tools/llvm-ml/CMakeLists.txt +++ b/llvm/tools/llvm-ml/CMakeLists.txt @@ -5,9 +5,15 @@ AllTargetsInfos MC MCParser + Option Support ) +set(LLVM_TARGET_DEFINITIONS Opts.td) + +tablegen(LLVM Opts.inc -gen-opt-parser-defs) +add_public_tablegen_target(MLTableGen) + add_llvm_tool(llvm-ml llvm-ml.cpp Disassembler.cpp diff --git a/llvm/tools/llvm-ml/Opts.td b/llvm/tools/llvm-ml/Opts.td new file mode 100644 --- /dev/null +++ b/llvm/tools/llvm-ml/Opts.td @@ -0,0 +1,108 @@ +include "llvm/Option/OptParser.td" + +// For LLVM-specific options, we prefer a two-dash prefix, but accept one for +// compatibility with llvm-mc. For clear separation from ML.EXE compatible +// options, slash is not accepted. +class LLVMFlag : Flag<["--", "-"], name>; +class LLVMJoined : Joined<["--", "-"], name>; +class LLVMJoinedOrSeparate : JoinedOrSeparate<["--", "-"], name>; +class LLVMSeparate : Separate<["--", "-"], name>; + +def ml_Group : OptionGroup<"">, + HelpText<"ML.EXE COMPATIBILITY OPTIONS">; +class MLFlag : Flag<["/", "-"], name>, Group; +class MLJoined : Joined<["/", "-"], name>, Group; +class MLJoinedOrSeparate : JoinedOrSeparate<["/", "-"], name>, + Group; +class MLSeparate : Separate<["/", "-"], name>, Group; + +def unsupported_Group : OptionGroup<"unsupported">, Flags<[HelpHidden]>, + HelpText<"UNSUPPORTED ML.EXE COMPATIBILITY OPTIONS">; +class UnsupportedFlag : Flag<["/", "-"], name>, + Group; +class UnsupportedJoined : Joined<["/", "-"], name>, + Group; +class UnsupportedJoinedOrSeparate : + JoinedOrSeparate<["/", "-"], name>, Group; +class UnsupportedSeparate : Separate<["/", "-"], name>, + Group; + +def help : MLFlag<"?">, + HelpText<"Display available options">; +def help_long : MLFlag<"help">, Alias; +def assemble_only : MLFlag<"c">, HelpText<"Assemble only; do not link">; +def output_file : MLJoinedOrSeparate<"Fo">, HelpText<"Names the output file">; +def include_path : MLJoinedOrSeparate<"I">, + HelpText<"Sets path for include files">; +def safeseh : MLFlag<"safeseh">, + HelpText<"Mark resulting object files as either containing no " + "exception handlers or containing exception handlers " + "that are all declared with .SAFESEH. Only available in " + "32-bit.">; +def assembly_file : MLJoinedOrSeparate<"Ta">, + HelpText<"Assemble source file with name not ending with " + "the .asm extension">; + +def bitness : LLVMJoined<"m">, Values<"32,64">, + HelpText<"Target platform (x86 or x86-64)">; +def as_lex : LLVMFlag<"as-lex">, + HelpText<"Lex tokens from a .asm file without assembling">; +def filetype : LLVMJoined<"filetype=">, Values<"obj,s,null">, + HelpText<"Emit a file with the given type">; +def output_att_asm : LLVMFlag<"output-att-asm">, + HelpText<"Use ATT syntax for output assembly">; +def show_encoding : LLVMFlag<"show-encoding">, + HelpText<"Show instruction encodings in output assembly">; +def show_inst : LLVMFlag<"show-inst">, + HelpText<"Show internal instruction representation in output " + "assembly">; +def show_inst_operands : LLVMFlag<"show-inst-operands">, + HelpText<"Show instructions operands as parsed">; +def print_imm_hex : LLVMFlag<"print-imm-hex">, + HelpText<"Prefer hex format for immediate values in output " + "assembly">; +def preserve_comments : LLVMFlag<"preserve-comments">, + HelpText<"Preserve comments in output assembly">; +def save_temp_labels : LLVMFlag<"save-temp-labels">, + HelpText<"Don't discard temporary labels">; + +def tiny_model_support : UnsupportedFlag<"AT">, HelpText<"">; +def alternate_linker : UnsupportedJoined<"Bl">, HelpText<"">; +def coff_object_file : UnsupportedFlag<"coff">, HelpText<"">; +def preserve_identifier_case : UnsupportedFlag<"Cp">, HelpText<"">; +def uppercase_identifiers : UnsupportedFlag<"Cu">, HelpText<"">; +def preserve_extern_case : UnsupportedFlag<"Cx">, HelpText<"">; +def define : UnsupportedJoinedOrSeparate<"D">, HelpText<"">; +def output_preprocessed : UnsupportedFlag<"EP">, HelpText<"">; +def errorreport : UnsupportedJoined<"ERRORREPORT">, HelpText<"">; +def stacksize : UnsupportedSeparate<"F">, HelpText<"">; +def executable_file : UnsupportedSeparate<"Fe">, HelpText<"">; +def code_listing_file : UnsupportedJoined<"FI">, HelpText<"">; +def linker_map_file : UnsupportedJoined<"Fm">, HelpText<"">; +def fp_emulator_fixups : UnsupportedFlag<"FPi">, HelpText<"">; +def source_browser_file : UnsupportedJoined<"Fr">, HelpText<"">; +def extended_source_browser_file : UnsupportedJoined<"FR">, HelpText<"">; +def pascal_conventions : UnsupportedFlag<"Gc">, HelpText<"">; +def c_conventions : UnsupportedFlag<"Gd">, HelpText<"">; +def stdcall_conventions : UnsupportedFlag<"GZ">, HelpText<"">; +def extern_name_limit : UnsupportedSeparate<"H">, HelpText<"">; +def nologo : UnsupportedFlag<"nologo">, HelpText<"">; +def omf_object_file : UnsupportedFlag<"omf">, HelpText<"">; +def full_listing : UnsupportedFlag<"Sa">, HelpText<"">; +def first_pass_listing : UnsupportedFlag<"Sf">, HelpText<"">; +def listing_width : UnsupportedSeparate<"SI">, HelpText<"">; +def listing_without_symbols : UnsupportedFlag<"Sn">, HelpText<"">; +def listing_page_length : UnsupportedSeparate<"Sp">, HelpText<"">; +def listing_subtitle : UnsupportedSeparate<"Ss">, HelpText<"">; +def listing_title : UnsupportedSeparate<"St">, HelpText<"">; +def listing_false_conditionals : UnsupportedFlag<"Sx">, HelpText<"">; +def extra_warnings : UnsupportedFlag<"w">, HelpText<"">; +def warning_level : UnsupportedJoined<"W">, HelpText<"">; +def error_on_warning : UnsupportedFlag<"WX">, HelpText<"">; +def ignore_include_envvar : UnsupportedFlag<"X">, HelpText<"">; +def line_number_info : UnsupportedFlag<"Zd">, HelpText<"">; +def export_all_symbols : UnsupportedFlag<"Zf">, HelpText<"">; +def codeview_info : UnsupportedFlag<"Zi">, HelpText<"">; +def enable_m510_option : UnsupportedFlag<"Zm">, HelpText<"">; +def structure_packing : UnsupportedJoined<"Zp">, HelpText<"">; +def parse_only : UnsupportedFlag<"Zs">, HelpText<"">; diff --git a/llvm/tools/llvm-ml/llvm-ml.cpp b/llvm/tools/llvm-ml/llvm-ml.cpp --- a/llvm/tools/llvm-ml/llvm-ml.cpp +++ b/llvm/tools/llvm-ml/llvm-ml.cpp @@ -10,8 +10,7 @@ // //===----------------------------------------------------------------------===// -#include "Disassembler.h" - +#include "llvm/ADT/StringSwitch.h" #include "llvm/MC/MCAsmBackend.h" #include "llvm/MC/MCAsmInfo.h" #include "llvm/MC/MCCodeEmitter.h" @@ -26,13 +25,17 @@ #include "llvm/MC/MCStreamer.h" #include "llvm/MC/MCSubtargetInfo.h" #include "llvm/MC/MCTargetOptionsCommandFlags.h" -#include "llvm/Support/CommandLine.h" +#include "llvm/Option/Arg.h" +#include "llvm/Option/ArgList.h" +#include "llvm/Option/Option.h" #include "llvm/Support/Compression.h" #include "llvm/Support/FileUtilities.h" +#include "llvm/Support/FormatVariadic.h" #include "llvm/Support/FormattedStream.h" #include "llvm/Support/Host.h" #include "llvm/Support/InitLLVM.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/Path.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/TargetRegistry.h" #include "llvm/Support/TargetSelect.h" @@ -40,131 +43,55 @@ #include "llvm/Support/WithColor.h" using namespace llvm; +using namespace llvm::opt; -static mc::RegisterMCTargetOptionsFlags MOF; - -static cl::opt -InputFilename(cl::Positional, cl::desc(""), cl::init("-")); - -static cl::opt -OutputFilename("o", cl::desc("Output filename"), cl::value_desc("filename"), - cl::init("-")); - -static cl::opt -ShowEncoding("show-encoding", cl::desc("Show instruction encodings")); - -static cl::opt -ShowInst("show-inst", cl::desc("Show internal instruction representation")); +namespace { -static cl::opt -ShowInstOperands("show-inst-operands", - cl::desc("Show instructions operands as parsed")); - -static cl::opt -OutputATTAsm("output-att-asm", cl::desc("Use ATT syntax for output printing")); - -static cl::opt -PrintImmHex("print-imm-hex", cl::init(false), - cl::desc("Prefer hex format for immediate values")); - -static cl::opt -PreserveComments("preserve-comments", - cl::desc("Preserve Comments in outputted assembly")); - -enum OutputFileType { - OFT_Null, - OFT_AssemblyFile, - OFT_ObjectFile +enum ID { + OPT_INVALID = 0, // This is not an option ID. +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + OPT_##ID, +#include "Opts.inc" +#undef OPTION }; -static cl::opt -FileType("filetype", cl::init(OFT_ObjectFile), - cl::desc("Choose an output file type:"), - cl::values( - clEnumValN(OFT_AssemblyFile, "asm", - "Emit an assembly ('.s') file"), - clEnumValN(OFT_Null, "null", - "Don't emit anything (for timing purposes)"), - clEnumValN(OFT_ObjectFile, "obj", - "Emit a native object ('.o') file"))); - -static cl::list -IncludeDirs("I", cl::desc("Directory of include files"), - cl::value_desc("directory"), cl::Prefix); - -enum BitnessType { - m32, - m64, + +#define PREFIX(NAME, VALUE) const char *const NAME[] = VALUE; +#include "Opts.inc" +#undef PREFIX + +static const opt::OptTable::Info InfoTable[] = { +#define OPTION(PREFIX, NAME, ID, KIND, GROUP, ALIAS, ALIASARGS, FLAGS, PARAM, \ + HELPTEXT, METAVAR, VALUES) \ + { \ + PREFIX, NAME, HELPTEXT, \ + METAVAR, OPT_##ID, opt::Option::KIND##Class, \ + PARAM, FLAGS, OPT_##GROUP, \ + OPT_##ALIAS, ALIASARGS, VALUES}, +#include "Opts.inc" +#undef OPTION }; -cl::opt Bitness(cl::desc("Choose bitness:"), cl::init(m64), - cl::values(clEnumVal(m32, "32-bit"), - clEnumVal(m64, "64-bit (default)"))); - -static cl::opt SafeSEH( - "safeseh", - cl::desc("Mark resulting object files as either containing no " - "exception handlers or containing exception handlers that " - "are all declared with .SAFESEH. Only available in 32-bit.")); - -static cl::opt -TripleName("triple", cl::desc("Target triple to assemble for, " - "see -version for available targets")); - -static cl::opt -DebugCompilationDir("fdebug-compilation-dir", - cl::desc("Specifies the debug info's compilation dir")); - -static cl::list -DebugPrefixMap("fdebug-prefix-map", - cl::desc("Map file source paths in debug info"), - cl::value_desc("= separated key-value pairs")); - -static cl::opt -MainFileName("main-file-name", - cl::desc("Specifies the name we should consider the input file")); - -static cl::opt SaveTempLabels("save-temp-labels", - cl::desc("Don't discard temporary labels")); - -enum ActionType { - AC_AsLex, - AC_Assemble, - AC_Disassemble, - AC_MDisassemble, + +class MLOptTable : public opt::OptTable { +public: + MLOptTable() : OptTable(InfoTable, true) {} }; +} // namespace -static cl::opt -Action(cl::desc("Action to perform:"), - cl::init(AC_Assemble), - cl::values(clEnumValN(AC_AsLex, "as-lex", - "Lex tokens from a .asm file"), - clEnumValN(AC_Assemble, "assemble", - "Assemble a .asm file (default)"), - clEnumValN(AC_Disassemble, "disassemble", - "Disassemble strings of hex bytes"), - clEnumValN(AC_MDisassemble, "mdis", - "Marked up disassembly of strings of hex bytes"))); - -static const Target *GetTarget(const char *ProgName) { +static Triple GetTriple(StringRef ProgName, opt::InputArgList &Args) { // Figure out the target triple. - if (TripleName.empty()) { - if (Bitness == m32) - TripleName = "i386-pc-windows"; - else if (Bitness == m64) - TripleName = "x86_64-pc-windows"; - } - Triple TheTriple(Triple::normalize(TripleName)); - - // Get the target specific parser. - std::string Error; - const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error); - if (!TheTarget) { - WithColor::error(errs(), ProgName) << Error; - return nullptr; - } - - // Update the triple name and return the found target. - TripleName = TheTriple.getTriple(); - return TheTarget; + StringRef DefaultBitness = "32"; + SmallString<255> Program = ProgName; + sys::path::replace_extension(Program, ""); + if (Program.endswith("ml64")) + DefaultBitness = "64"; + + StringRef TripleName = + StringSwitch(Args.getLastArgValue(OPT_bitness, DefaultBitness)) + .Case("32", "i386-pc-windows") + .Case("64", "x86_64-pc-windows") + .Default(""); + return Triple(Triple::normalize(TripleName)); } static std::unique_ptr GetOutputStream(StringRef Path) { @@ -197,10 +124,11 @@ return Error; } -static int AssembleInput(const char *ProgName, const Target *TheTarget, +static int AssembleInput(StringRef ProgName, const Target *TheTarget, SourceMgr &SrcMgr, MCContext &Ctx, MCStreamer &Str, MCAsmInfo &MAI, MCSubtargetInfo &STI, - MCInstrInfo &MCII, MCTargetOptions &MCOptions) { + MCInstrInfo &MCII, MCTargetOptions &MCOptions, + const opt::ArgList &InputArgs) { std::unique_ptr Parser( createMCMasmParser(SrcMgr, Ctx, Str, MAI, 0)); std::unique_ptr TAP( @@ -212,7 +140,7 @@ return 1; } - Parser->setShowParsedOperands(ShowInstOperands); + Parser->setShowParsedOperands(InputArgs.hasArg(OPT_show_inst_operands)); Parser->setTargetParser(*TAP); Parser->getLexer().setLexMasmIntegers(true); Parser->getLexer().useMasmDefaultRadix(true); @@ -224,8 +152,9 @@ return Res; } -int main(int argc, char **argv) { - InitLLVM X(argc, argv); +int main(int Argc, char **Argv) { + InitLLVM X(Argc, Argv); + StringRef ProgName = sys::path::filename(Argv[0]); // Initialize targets and assembly printers/parsers. llvm::InitializeAllTargetInfos(); @@ -233,21 +162,75 @@ llvm::InitializeAllAsmParsers(); llvm::InitializeAllDisassemblers(); - // Register the target printer for --version. - cl::AddExtraVersionPrinter(TargetRegistry::printRegisteredTargetsForVersion); + MLOptTable T; + unsigned MissingArgIndex, MissingArgCount; + ArrayRef ArgsArr = makeArrayRef(Argv + 1, Argc - 1); + opt::InputArgList InputArgs = + T.ParseArgs(ArgsArr, MissingArgIndex, MissingArgCount); + + std::string InputFilename; + for (auto *Arg : InputArgs.filtered(OPT_INPUT)) { + std::string ArgString = Arg->getAsString(InputArgs); + if (ArgString == "-" || StringRef(ArgString).endswith(".asm")) { + if (!InputFilename.empty()) { + WithColor::warning(errs(), ProgName) + << "does not support multiple assembly files in one command; " + << "ignoring '" << InputFilename << "'\n"; + } + InputFilename = ArgString; + } else { + std::string Diag; + raw_string_ostream OS(Diag); + OS << "invalid option '" << ArgString << "'"; + + std::string Nearest; + if (T.findNearest(ArgString, Nearest) < 2) + OS << ", did you mean '" << Nearest << "'?"; + + WithColor::error(errs(), ProgName) << OS.str() << '\n'; + exit(1); + } + } + for (auto *Arg : InputArgs.filtered(OPT_assembly_file)) { + if (!InputFilename.empty()) { + WithColor::warning(errs(), ProgName) + << "does not support multiple assembly files in one command; " + << "ignoring '" << InputFilename << "'\n"; + } + InputFilename = Arg->getAsString(InputArgs); + } + + for (auto &Arg : InputArgs.filtered(OPT_unsupported_Group)) { + WithColor::warning(errs(), ProgName) + << "ignoring unsupported '" << Arg->getOption().getName() + << "' option\n"; + } + + if (InputArgs.hasArg(OPT_help)) { + std::string Usage = llvm::formatv("{0} [ /options ] file", ProgName).str(); + T.PrintHelp(outs(), Usage.c_str(), "LLVM MASM Assembler", + /*ShowHidden=*/false); + return 0; + } else if (InputFilename.empty()) { + outs() << "USAGE: " << ProgName << " [ /options ] file\n" + << "Run \"" << ProgName << " /?\" or \"" << ProgName + << " /help\" for more info.\n"; + return 0; + } - cl::ParseCommandLineOptions(argc, argv, "llvm machine code playground\n"); - MCTargetOptions MCOptions = mc::InitMCTargetOptionsFromFlags(); + MCTargetOptions MCOptions; MCOptions.AssemblyLanguage = "masm"; - const char *ProgName = argv[0]; - const Target *TheTarget = GetTarget(ProgName); - if (!TheTarget) + Triple TheTriple = GetTriple(ProgName, InputArgs); + std::string Error; + const Target *TheTarget = TargetRegistry::lookupTarget("", TheTriple, Error); + if (!TheTarget) { + WithColor::error(errs(), ProgName) << Error; return 1; - // Now that GetTarget() has (potentially) replaced TripleName, it's safe to - // construct the Triple object. - Triple TheTriple(TripleName); + } + const std::string &TripleName = TheTriple.getTriple(); + bool SafeSEH = InputArgs.hasArg(OPT_safeseh); if (SafeSEH && !(TheTriple.isArch32Bit() && TheTriple.isX86())) { WithColor::warning() << "/safeseh applies only to 32-bit X86 platforms; ignoring.\n"; @@ -261,7 +244,6 @@ << InputFilename << ": " << EC.message() << '\n'; return 1; } - MemoryBuffer *Buffer = BufferPtr->get(); SourceMgr SrcMgr; @@ -270,7 +252,7 @@ // Record the location of the include directories so that the lexer can find // it later. - SrcMgr.setIncludeDirs(IncludeDirs); + SrcMgr.setIncludeDirs(InputArgs.getAllArgValues(OPT_include_path)); std::unique_ptr MRI(TheTarget->createMCRegInfo(TripleName)); assert(MRI && "Unable to create target register info!"); @@ -279,7 +261,7 @@ TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); assert(MAI && "Unable to create target asm info!"); - MAI->setPreserveAsmComments(PreserveComments); + MAI->setPreserveAsmComments(InputArgs.hasArg(OPT_preserve_comments)); // FIXME: This is not pretty. MCContext has a ptr to MCObjectFileInfo and // MCObjectFileInfo needs a MCContext reference in order to initialize itself. @@ -288,24 +270,25 @@ MOFI.InitMCObjectFileInfo(TheTriple, /*PIC=*/false, Ctx, /*LargeCodeModel=*/true); - if (SaveTempLabels) + if (InputArgs.hasArg(OPT_save_temp_labels)) Ctx.setAllowTemporaryLabels(false); - if (!DebugCompilationDir.empty()) { - Ctx.setCompilationDir(DebugCompilationDir); + // Set compilation information. + SmallString<128> CWD; + if (!sys::fs::current_path(CWD)) + Ctx.setCompilationDir(CWD); + Ctx.setMainFileName(InputFilename); + + StringRef FileType = InputArgs.getLastArgValue(OPT_filetype, "obj"); + SmallString<255> DefaultOutputFilename; + if (InputArgs.hasArg(OPT_as_lex)) { + DefaultOutputFilename = "-"; } else { - // If no compilation dir is set, try to use the current directory. - SmallString<128> CWD; - if (!sys::fs::current_path(CWD)) - Ctx.setCompilationDir(CWD); - } - for (const auto &Arg : DebugPrefixMap) { - const auto &KV = StringRef(Arg).split('='); - Ctx.addDebugPrefixMapEntry(std::string(KV.first), std::string(KV.second)); + DefaultOutputFilename = InputFilename; + sys::path::replace_extension(DefaultOutputFilename, FileType); } - if (!MainFileName.empty()) - Ctx.setMainFileName(MainFileName); - + const StringRef OutputFilename = + InputArgs.getLastArgValue(OPT_output_file, DefaultOutputFilename); std::unique_ptr Out = GetOutputStream(OutputFilename); if (!Out) return 1; @@ -322,11 +305,12 @@ assert(STI && "Unable to create subtarget info!"); MCInstPrinter *IP = nullptr; - if (FileType == OFT_AssemblyFile) { + if (FileType == "s") { + const bool OutputATTAsm = InputArgs.hasArg(OPT_output_att_asm); const unsigned OutputAsmVariant = OutputATTAsm ? 0U // ATT dialect : 1U; // Intel dialect - IP = TheTarget->createMCInstPrinter(Triple(TripleName), OutputAsmVariant, - *MAI, *MCII, *MRI); + IP = TheTarget->createMCInstPrinter(TheTriple, OutputAsmVariant, *MAI, + *MCII, *MRI); if (!IP) { WithColor::error() @@ -337,26 +321,24 @@ } // Set the display preference for hex vs. decimal immediates. - IP->setPrintImmHex(PrintImmHex); + IP->setPrintImmHex(InputArgs.hasArg(OPT_print_imm_hex)); // Set up the AsmStreamer. std::unique_ptr CE; - if (ShowEncoding) + if (InputArgs.hasArg(OPT_show_encoding)) CE.reset(TheTarget->createMCCodeEmitter(*MCII, *MRI, Ctx)); std::unique_ptr MAB( TheTarget->createMCAsmBackend(*STI, *MRI, MCOptions)); auto FOut = std::make_unique(*OS); - Str.reset( - TheTarget->createAsmStreamer(Ctx, std::move(FOut), /*asmverbose*/ true, - /*useDwarfDirectory*/ true, IP, - std::move(CE), std::move(MAB), ShowInst)); + Str.reset(TheTarget->createAsmStreamer( + Ctx, std::move(FOut), /*asmverbose*/ true, + /*useDwarfDirectory*/ true, IP, std::move(CE), std::move(MAB), + InputArgs.hasArg(OPT_show_inst))); - } else if (FileType == OFT_Null) { + } else if (FileType == "null") { Str.reset(TheTarget->createNullStreamer(Ctx)); - } else { - assert(FileType == OFT_ObjectFile && "Invalid file type!"); - + } else if (FileType == "obj") { if (!Out->os().supportsSeeking()) { BOS = std::make_unique(Out->os()); OS = BOS.get(); @@ -369,6 +351,8 @@ MAB->createObjectWriter(*OS), std::unique_ptr(CE), *STI, MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible, /*DWARFMustBeAtTheEnd*/ false)); + } else { + llvm_unreachable("Invalid file type!"); } if (TheTriple.isOSBinFormatCOFF()) { @@ -392,27 +376,13 @@ Str->setUseAssemblerInfoForParsing(true); int Res = 1; - bool disassemble = false; - switch (Action) { - case AC_AsLex: + if (InputArgs.hasArg(OPT_as_lex)) { + // -as-lex; Lex only, and output a stream of tokens Res = AsLexInput(SrcMgr, *MAI, Out->os()); - break; - case AC_Assemble: + } else { Res = AssembleInput(ProgName, TheTarget, SrcMgr, Ctx, *Str, *MAI, *STI, - *MCII, MCOptions); - break; - case AC_MDisassemble: - assert(IP && "Expected assembly output"); - IP->setUseMarkup(1); - disassemble = true; - break; - case AC_Disassemble: - disassemble = true; - break; + *MCII, MCOptions, InputArgs); } - if (disassemble) - Res = Disassembler::disassemble(*TheTarget, TripleName, *STI, *Str, *Buffer, - SrcMgr, Out->os()); // Keep output if no errors. if (Res == 0)