MISRA-C diary(C言語日誌)

2017年6月の記事一覧

OSX の gcc を更新


OSX の gcc を変更した話 ports

macのgcc(clang)からgcc6.3にしたい


brew install gcc

Warning: gcc 7.1.0 is already installed

gcc -v

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1

Apple LLVM version 8.1.0 (clang-802.0.42)

Target: x86_64-apple-darwin16.6.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin



0

きつねさんでもわかるLLVM コンパイラを自作するためのガイドブック第5章

きつねさんでもわかるLLVM ~コンパイラを自作するためのガイドブック~
柏木 餅子, 風薬
インプレス(2013/06/21)






p.59
primary_expression
: VARIABLE_IDENTIFIER
| CONSTANT
| '(' , expression , ')'
;

postfix_expression
: primary_expression
| FUNCTION_IDENTIFIER , '(' , [ argument_list  ] , ')'
;

multiplicative_expression
: postfix_expression , [ { "*" , postfix_expression  |  "/" , postfix_expression } ]
;

additive_expression
: multiplicative_expression , [ { "+" , multiplicative_expression  |  "-" , multiplicative_expression } ]
;

assignment_expression
: IDENTIFIER , '=' , additive_expression
| additive_expression
;

argument_list
: assignment_expression , [ { "," , assignment_expression } ]
;

parameter_list
: parameter , [ { "," , parameter } ]
;

parameter
: INT , IDENTIFIER
;

statement
: assignment_statement
| jump_statement
;

variable_declaration_list
: { variable_declaration }
;

statement_list
: { statement }
;

expression_statement
: ';'
| assignment_expression , ';'
;

jump_statement
: RETURN , assignment_expression , ';'
;

function_statement
: "{" , [ variable_declaration , list ] , statement_list , "}"
;

external_declaration
: function_declaration 
| function_definition
;

variable_declaration
: INT , IDENTIFIER , ";"
;

prototype
: type_specifier , IDENTIFIER , '(' , [ parameter , { "," , parameter} ] , ')' 
;

function_declaration
: prototype , ";"
;

function_declaration
: prototype , function statement
;

translation_unit
: { external_declaration }
;

p.62
int test(int j){
int I;
 i=j*10;
return I;
}
int main(){
int I;
=10;                                                                                                             printnum(test(i));
return 0;
 }
0

きつねさんでもわかるLLVM コンパイラを自作するためのガイドブック第4章

きつねさんでもわかるLLVM ~コンパイラを自作するためのガイドブック~
柏木 餅子, 風薬
インプレス(2013/06/21)






4.5 基本的なLLVM の中間表現.
p.41

#include <stdio.h>

int main(){

printf("HelloWorld\n");
return 0;

}

$ clang -emit-llvm -S -o helloworld.ll helloworld.c

$ cat hello world.ll

; ModuleID = 'helloworld.c'

source_filename = "helloworld.c"

target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"

target triple = "x86_64-apple-macosx10.12.0"


@.str = private unnamed_addr constant [12 x i8] c"HelloWorld\0A\00", align 1


; Function Attrs: nounwind ssp uwtable

define i32 @main() #0 {

  %1 = alloca i32, align 4

  store i32 0, i32* %1, align 4

  %2 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i32 0, i32 0))

  ret i32 0

}


declare i32 @printf(i8*, ...) #1


attributes #0 = { nounwind ssp uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }

attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="penryn" "target-features"="+cx16,+fxsr,+mmx,+sse,+sse2,+sse3,+sse4.1,+ssse3,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }


!llvm.module.flags = !{!0}

!llvm.ident = !{!1}


!0 = !{i32 1, !"PIC Level", i32 2}

!1 = !{!"Apple LLVM version 8.1.0 (clang-802.0.42)"}



0

でき


きつねさんでもわかるLLVM 達人出版会 1,150円+税   2012-2014
0

LLVM

LLVM

1.

cat hoge.c

int main(int x){

return printf("%d\r",x)

}


2.
clang hoge.c

hoge.c:1:5: warning: only one parameter on 'main' declaration [-Wmain]

int main(int x){

    ^

hoge.c:2:9: warning: implicitly declaring library function 'printf' with type

      'int (const char *, ...)' [-Wimplicit-function-declaration]

        return printf("%d\r",x)

               ^

hoge.c:2:9: note: include the header <stdio.h> or explicitly provide a declaration for

      'printf'

hoge.c:2:25: error: expected ';' after return statement

        return printf("%d\r",x)

                               ^

                               ;

2 warnings and 1 error generated.

3.

#include <stdio.h>
int main(int argc, char** argv){

return printf("%d\r",argc);

}


4.

#include <stdio.h>

int main(int argc, char** argv){

return printf("argc = %d\n",argc);

}

$ clang hoge3.c
$ ./a.out

argc = 1

5

clang -v

Apple LLVM version 8.1.0 (clang-802.0.42)

Target: x86_64-apple-darwin16.6.0

Thread model: posix

InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin



0