changeset 26:8550260c18fa

fix aarch on mac and llvm
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 18 Oct 2024 16:06:51 +0900
parents e84ea6c0d359
children c4e2ca100ab9
files calc.c s-code-arm-mac.c s-code-llvm.c
diffstat 3 files changed, 87 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/calc.c	Fri Oct 11 15:33:59 2024 +0900
+++ b/calc.c	Fri Oct 18 16:06:51 2024 +0900
@@ -30,8 +30,7 @@
 }
 
 static void
-error(description)
-char *description;
+error(char *description)
 {
     fprintf(stderr,"%s on %s\n",description,last_ptr);
     line_skip();
--- a/s-code-arm-mac.c	Fri Oct 11 15:33:59 2024 +0900
+++ b/s-code-arm-mac.c	Fri Oct 18 16:06:51 2024 +0900
@@ -46,6 +46,7 @@
 
 static
 char *ending[] = {
+"            bl _exit\n"
 "            .loh AdrpLdr    Lloh0, Lloh1\n"
 "            .cfi_endproc\n"
 "                                            ; -- End function\n"
@@ -56,9 +57,13 @@
 "            .globl  _v                              ; @a\n"
 "            .p2align        3\n"
 "    _a:\n"
-"            .quad   64                               ; 64\n"
+"            .quad   128                               ; 128\n"
+"            .quad   128                               ; 128\n"
 "    _v:\n"
 "            .quad   64*100                               ; 64\n"
+"            .quad   64*100                               ; 64\n"
+"            .quad   64*100                               ; 64\n"
+"            .quad   64*100                               ; 64\n"
 "\n"
 "    .subsections_via_symbols\n",
  NULL
@@ -67,29 +72,32 @@
 void
 emit_push()
 {
-    printf("\tstr x0,[sp, -8]!\n");
+    printf("\tstr x0,[sp, -16]!\n");
 }
 
 void
 emit_compare()
 {
     printf("\tldr x1,[sp]\n");
-    printf("\tadd sp,sp,#8\n");
+    printf("\tadd sp,sp,#16\n");
     printf("\tcmp x0,x1\n");
 }
 
 void
 emit_store( int d )
 {
-    printf("add     x9, x8, _v@PAGEOFF\n");
-    printf("str     x0, [x9, %d ]\n",d);
+    printf("    adrp    x9, _v@GOTPAGE\n");
+    printf("    ldr     x9, [x9, _v@GOTPAGEOFF]\n");
+    printf("str     x0, [x9, %d ]\n",d * 0x10);
+
 }
 
 void
 emit_load(int d)
 {
-    printf("add     x9, x8, _v@PAGEOFF\n");
-    printf("ldr     x0, [x9, %d ]\n",d);
+    printf("    adrp    x9, _v@GOTPAGE\n");
+    printf("    ldr     x9, [x9, _v@GOTPAGEOFF]\n");
+    printf("ldr     x0, [x9, %d ]\n",d * 0x10);
 }
 
 
@@ -113,26 +121,25 @@
 {
     if(op==O_DIV ) {
         printf("\tldr x1,[sp]\n");
-        printf("\tadd sp,sp,8\n");
+        printf("\tadd sp,sp,16\n");
         printf("\tsdiv x0 ,x1 , x0\n");
     } else if( op==O_DIV_R ) {
         printf("\tldr x1,[sp]\n");
-        printf("\tadd sp,sp,#8\n");
+        printf("\tadd sp,sp,#16\n");
         printf("\tsdiv x1 ,x0 , x0\n");
     } else if(op==O_SUB) {
         printf("\tldr x1,[sp]\n");
-        printf("\tadd sp,sp,#8\n");
+        printf("\tadd sp,sp,#16\n");
         printf("\tsub x1 ,x0 , x0\n");
     } else {
         printf("\tldr x1,[sp]\n");
-        printf("\tadd sp,sp,#8\n");
+        printf("\tadd sp,sp,#16\n");
         printf("\t%s x1 ,x0 , x0\n",opcode[op]);
     }
 }
 
 void
-emit_value(d)
-int d;
+emit_value(int d)
 {
     printf("\tmov x0, #%d\n",d);  // 16bit ?
 }
--- a/s-code-llvm.c	Fri Oct 11 15:33:59 2024 +0900
+++ b/s-code-llvm.c	Fri Oct 18 16:06:51 2024 +0900
@@ -9,34 +9,72 @@
 char *comments = "; ";
 static
 char *intro[] = {
-   "target datalayout = \"e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128\"\n",
-    "target triple = \"x86_64-apple-macosx10.10.0\"\n",
-    "@.str = private unnamed_addr constant [5 x i8] c\"\0A = \00\", align 1\n",
-    "define i32 @main() nounwind uwtable ssp {\n",
-    "entry:\n",
+    "; ModuleID = 'tmp.c'\n",
+    "source_filename = \"tmp.c\"\n",
+    "target datalayout = \"e-m:o-i64:64-i128:128-n32:64-S128\"\n",
+    "target triple = \"arm64-apple-macosx14.0.0"\n",
+    "\n",
+    "@a = global i64 4, align 8\n",
+    "@b = global i64 3, align 8\n",
+    "@p = global i64 3, align 8\n",
+    "@.str = private unnamed_addr constant [5 x i8] c\"%ld\0A\00\", align 1\n",
+    "@v = common global [100 x i64] zeroinitializer, align 8\n",
+    "\n",
+    "; Function Attrs: noinline nounwind optnone ssp uwtable(sync)\n",
+    "define void @print(i64 noundef %0) #0 {\n",
+    "  %2 = alloca i64, align 8\n",
+    "  store i64 %0, ptr %2, align 8\n",
+    "  %3 = load i64, ptr %2, align 8\n",
+    "  %4 = call i32 (ptr, ...) @printf(ptr noundef @.str, i64 noundef %3)\n",
+    "  ret void\n",
+    "}\n",
+    "\n",
+    "declare i32 @printf(ptr noundef, ...) #1\n",
+    "\n",
+    "; Function Attrs: noinline nounwind optnone ssp uwtable(sync)\n",
+    "define i32 @main() #0 {\n",
+    "  %1 = alloca i32, align 4\n",
+    "  store i32 0, ptr %1, align 4\n",
+    "  %2 = load i64, ptr @a, align 8\n",
+    "  %3 = add nsw i64 %2, 1\n",
+    "  %4 = load i64, ptr @b, align 8\n",
+    "  %5 = sub nsw i64 %4, 123\n",
+    "  %6 = sub nsw i64 %3, %5\n",
+    "",
   NULL
 };
 
+
 static
 char *ending[] = {
-   "  ret i32 0\n",
-   "}\n",
-   "declare i32 @printf(i8*, ...)\n",
-   "; Function Attrs: nounwind ssp uwtable",
-   "define i32 @main() #0 {",
-   "  %1 = load i32* @a, align 4",
-   "  %2 = load i32* @b, align 4",
-   "  %3 = add nsw i32 %1, %2",
-   "  call void @print(i32 %3)",
-   "  ret i32 0",
-   "}",
-   "",
-   "attributes #0 = { nounwind ssp uwtable \"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\" \"stack-protector-buffer-size\"=\"8\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }",
-    "attributes #1 = { \"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\" \"stack-protector-buffer-size\"=\"8\" \"unsafe-fp-math\"=\"false\" \"use-soft-float\"=\"false\" }",
-    "",
-    "!llvm.ident = !{!0}",
-    "",
-    "!0 = metadata !{metadata !\"Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)\"}",
+  "  store i64 %6, ptr @a, align 8\n",
+  "  %7 = load i64, ptr @a, align 8\n",
+  "  call void @print(i64 noundef %7)\n",
+  "  %8 = load i64, ptr @p, align 8\n",
+  "  %9 = getelementptr inbounds [100 x i64], ptr @v, i64 0, i64 %8\n",
+  "  store i64 3, ptr %9, align 8\n",
+  "  %10 = load i64, ptr @p, align 8\n",
+  "  %11 = getelementptr inbounds [100 x i64], ptr @v, i64 0, i64 %10\n",
+  "  %12 = load i64, ptr %11, align 8\n",
+  "  call void @print(i64 noundef %12)\n",
+  "  %13 = load i64, ptr @a, align 8\n",
+  "  %14 = trunc i64 %13 to i32\n",
+  "  ret i32 %14\n",
+  "}\n",
+  "\n",
+  "attributes #0 = { noinline nounwind optnone ssp uwtable(sync) \"frame-pointer\"=\"non-leaf\" \"min-legal-vector-width\"=\"0\" \"no-trapping-math\"=\"true\" \"probe-stack\"=\"__chkstk_darwin\" \"stack-protector-buffer-size\"=\"8\" \"target-cpu\"=\"apple-m1\" \"target-features\"=\"+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8a,+zcm,+zcz\" }\n",
+  "attributes #1 = { \"frame-pointer\"=\"non-leaf\" \"no-trapping-math\"=\"true\" \"probe-stack\"=\"__chkstk_darwin\" \"stack-protector-buffer-size\"=\"8\" \"target-cpu\"=\"apple-m1\" \"target-features\"=\"+aes,+crc,+crypto,+dotprod,+fp-armv8,+fp16fml,+fullfp16,+lse,+neon,+ras,+rcpc,+rdm,+sha2,+sha3,+sm4,+v8.1a,+v8.2a,+v8.3a,+v8.4a,+v8.5a,+v8a,+zcm,+zcz\" }\n",
+  "\n",
+  "!llvm.module.flags = !{!0, !1, !2, !3, !4}\n",
+  "!llvm.ident = !{!5}\n",
+  "\n",
+  "!0 = !{i32 2, !\"SDK Version\", [2 x i32] [i32 14, i32 5]}\n",
+  "!1 = !{i32 1, !\"wchar_size\", i32 4}\n",
+  "!2 = !{i32 8, !\"PIC Level\", i32 2}\n",
+  "!3 = !{i32 7, !\"uwtable\", i32 1}\n",
+  "!4 = !{i32 7, !\"frame-pointer\", i32 1}\n",
+  "!5 = !{!\"Apple clang version 15.0.0 (clang-1500.3.9.4)\"}\n",
+  "\n",
      NULL
 };
 
@@ -65,7 +103,7 @@
 }
 
 static void 
-free_register(i) {    /* いらなくなったレジスタを開放 */
+free_register(int i) {    /* いらなくなったレジスタを開放 */
     regs[i]=0;
 }
 
@@ -95,9 +133,9 @@
 static int 
 pop_register() 
 {     /* レジスタから値を取り出す */
-    int i,j;
+    int i;
 
-    j = creg;
+    // j = creg;
     i = reg_stack[--reg_sp];
 
     if(i<0) {
@@ -141,8 +179,7 @@
 }
 
 void
-emit_store(assign)
-int assign;
+emit_store(int assign)
 {
     int tmp = get_register();
 
@@ -193,8 +230,7 @@
 }
 
 void
-emit_value(d)
-int d;
+emit_value(int d)
 {
     printf("\t%%%d =  i32 %d\n",creg,d);
 }