Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

comments in assembly code cause assembler errors in some versions of MacOS on x86-64 #399

Closed
monniaux opened this issue Jun 9, 2021 · 7 comments

Comments

@monniaux
Copy link
Contributor

monniaux commented Jun 9, 2021

Some versions of MacOS install a gcc command that is actually aliased to clang. Some versions of that command run the C preprocessor on .s files, whereas gcc runs it only on .S files. This causes some mysterious errors when assembling these files when CompCert prints comment lines, which start with #, when these lines are identified as C preprocessor directives, which happens for instance if the comment line is of the form # if (blabla).

Fix: use ## as comment prefix.

diff --git a/x86/TargetPrinter.ml b/x86/TargetPrinter.ml
index 2000f96a..50c871e4 100644
--- a/x86/TargetPrinter.ml
+++ b/x86/TargetPrinter.ml
@@ -96,7 +96,7 @@ let z oc n = output_string oc (Z.to_string n)
 let data_pointer = if Archi.ptr64 then ".quad" else ".long"
 
 (* The comment deliminiter *)
-let comment = "#"
+let comment = "##"
 
 (* Base-2 log of a Caml integer *)
 let rec log2 n =
@xavierleroy
Copy link
Contributor

xavierleroy commented Jun 10, 2021

Thanks for the info. It is strange (bordering on "it is a bug") that clang would preprocess a .s file. .S files are to be preprocessed, of course, but why .s files?

I couldn't find any documentation on how clang treats assembly source files, however. Do you have any pointers you could share?

At any rate, I'm fine with changing the comment syntax on x86_64-macos, but perhaps not (yet) on x86_64-linux. The reason being that one of the two program annotation mechanisms of CompCert produce comments in assembly files that users are supposed to extract with their own tools, so the less we change the syntax of comments the less we break these hypothetical tools.

@xavierleroy
Copy link
Contributor

Tentative fix in a0ad5ff . Holler if it doesn't work.

@monniaux
Copy link
Contributor Author

monniaux commented Jun 11, 2021

For reference:

$ uname
Darwin
$ sw_vers
ProductName:        Mac OS X
ProductVersion:        10.13.6
BuildVersion:        17G14033

$ gcc -v
Configured with: --prefix=/Library/Developer/CommandLineTools/usr
--with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.10.44.4)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

If one tries to compile the file:

#  if (toto)

One gets:

$ gcc -c a.s
a.s:1:3: error: unterminated conditional directive
# if (toto)
   ^
1 error generated.

Also reported by another user with configuration:

$ gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/c++/4.2.1
Apple clang version 12.0.0 (clang-1200.0.32.29)
Target: x86_64-apple-darwin19.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

$ uname -a
Darwin eduroam-108087.grenet.fr 19.6.0 Darwin Kernel Version 19.6.0: Mon Apr 12 20:57:45 PDT 2021; root:xnu-6153.141.28.1~1/RELEASE_X86_64 x86_64

@m-schmidt
Copy link
Member

A simple compile of a .c file to .s also shows ## as comments for assembly:

.section	__TEXT,__text,regular,pure_instructions
	.build_version macos, 11, 0	sdk_version 11, 3
	.globl	_main                           ## -- Begin function main
	.p2align	4, 0x90
_main:                                  ## @main
	.cfi_startproc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants
@m-schmidt @monniaux @xavierleroy and others