authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-12 02:04:46-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-02-12 02:04:46-07:00
log592210a1739dfe3fe506260c11b8b5e5d8a5a715
treed858d66fc9e3fa84957c146187daa3ca74c6d337
parent0c1ce21f7d5f2756bc9e642c17092db9f9f2cf05

i386 support

closes #115 Thanks to Seo Sanghyeon for the port code.

4 files changed, 224 insertions(+), 27 deletions(-)

src/link.cpp+56-3
...@@ -80,14 +80,68 @@ static const char *get_exe_file_extension(CodeGen *g) {...@@ -80,14 +80,68 @@ static const char *get_exe_file_extension(CodeGen *g) {
80 }80 }
81}81}
8282
83static const char *getLDMOption(const ZigTarget *t) {
84 switch (t->arch.arch) {
85 case ZigLLVM_x86:
86 return "elf_i386";
87 case ZigLLVM_aarch64:
88 return "aarch64linux";
89 case ZigLLVM_aarch64_be:
90 return "aarch64_be_linux";
91 case ZigLLVM_arm:
92 case ZigLLVM_thumb:
93 return "armelf_linux_eabi";
94 case ZigLLVM_armeb:
95 case ZigLLVM_thumbeb:
96 return "armebelf_linux_eabi";
97 case ZigLLVM_ppc:
98 return "elf32ppclinux";
99 case ZigLLVM_ppc64:
100 return "elf64ppc";
101 case ZigLLVM_ppc64le:
102 return "elf64lppc";
103 case ZigLLVM_sparc:
104 case ZigLLVM_sparcel:
105 return "elf32_sparc";
106 case ZigLLVM_sparcv9:
107 return "elf64_sparc";
108 case ZigLLVM_mips:
109 return "elf32btsmip";
110 case ZigLLVM_mipsel:
111 return "elf32ltsmip";
112 return "elf64btsmip";
113 case ZigLLVM_mips64el:
114 return "elf64ltsmip";
115 case ZigLLVM_systemz:
116 return "elf64_s390";
117 case ZigLLVM_x86_64:
118 if (t->environ == ZigLLVM_GNUX32) {
119 return "elf32_x86_64";
120 }
121 return "elf_x86_64";
122 default:
123 zig_unreachable();
124 }
125}
126
83static void construct_linker_job_linux(LinkJob *lj) {127static void construct_linker_job_linux(LinkJob *lj) {
84 CodeGen *g = lj->codegen;128 CodeGen *g = lj->codegen;
85129
86 lj->args.append("-m");130 lj->args.append("-m");
87 lj->args.append("elf_x86_64");131 lj->args.append(getLDMOption(&g->zig_target));
88132
133 bool is_lib = g->out_type == OutTypeLib;
134 bool shared = !g->is_static && is_lib;
89 if (g->is_static) {135 if (g->is_static) {
90 lj->args.append("-static");136 if (g->zig_target.arch.arch == ZigLLVM_arm || g->zig_target.arch.arch == ZigLLVM_armeb ||
137 g->zig_target.arch.arch == ZigLLVM_thumb || g->zig_target.arch.arch == ZigLLVM_thumbeb)
138 {
139 lj->args.append("-Bstatic");
140 } else {
141 lj->args.append("-static");
142 }
143 } else if (shared) {
144 lj->args.append("-shared");
91 }145 }
92146
93 lj->args.append("-o");147 lj->args.append("-o");
...@@ -135,7 +189,6 @@ static void construct_linker_job_linux(LinkJob *lj) {...@@ -135,7 +189,6 @@ static void construct_linker_job_linux(LinkJob *lj) {
135 buf_appendf(&lj->out_file, "lib%s.so.%d.%d.%d",189 buf_appendf(&lj->out_file, "lib%s.so.%d.%d.%d",
136 buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch);190 buf_ptr(g->root_out_name), g->version_major, g->version_minor, g->version_patch);
137 Buf *soname = buf_sprintf("lib%s.so.%d", buf_ptr(g->root_out_name), g->version_major);191 Buf *soname = buf_sprintf("lib%s.so.%d", buf_ptr(g->root_out_name), g->version_major);
138 lj->args.append("-shared");
139 lj->args.append("-soname");192 lj->args.append("-soname");
140 lj->args.append(buf_ptr(soname));193 lj->args.append(buf_ptr(soname));
141 }194 }
std/bootstrap.zig+13-3
...@@ -9,9 +9,19 @@ var env: &&u8 = undefined;...@@ -9,9 +9,19 @@ var env: &&u8 = undefined;
99
10#attribute("naked")10#attribute("naked")
11export fn _start() -> unreachable {11export fn _start() -> unreachable {
12 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));12 switch (@compile_var("arch")) {
13 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));13 x86_64 => {
14 env = asm("lea 0x10(%%rsp,%%rdi,8), %[env]": [env] "=r" (-> &&u8));14 argc = asm("mov (%%rsp), %[argc]": [argc] "=r" (-> isize));
15 argv = asm("lea 0x8(%%rsp), %[argv]": [argv] "=r" (-> &&u8));
16 env = asm("lea 0x10(%%rsp,[argc],8), %[env]": [env] "=r" (-> &&u8): [argc] "r" (argc));
17 },
18 i386 => {
19 argc = asm("mov (%%esp), %[argc]": [argc] "=r" (-> isize));
20 argv = asm("lea 0x4(%%esp), %[argv]": [argv] "=r" (-> &&u8));
21 env = asm("lea 0x8(%%esp,%[argc],4), %[env]": [env] "=r" (-> &&u8): [argc] "r" (argc));
22 },
23 else => unreachable{},
24 }
15 call_main()25 call_main()
16}26}
1727
std/syscall.zig+149-20
...@@ -1,17 +1,63 @@...@@ -1,17 +1,63 @@
1// this file is specific to x86_641const SYS_read = switch (@compile_var("arch")) {
22 x86_64 => 0,
3const SYS_read = 0;3 i386 => 3,
4const SYS_write = 1;4 else => unreachable{},
5const SYS_mmap = 9;5};
6const SYS_munmap = 11;6const SYS_write = switch (@compile_var("arch")) {
7const SYS_rt_sigprocmask = 14;7 x86_64 => 1,
8const SYS_exit = 60;8 i386 => 4,
9const SYS_kill = 62;9 else => unreachable{},
10const SYS_getgid = 104;10};
11const SYS_gettid = 186;11const SYS_mmap = switch (@compile_var("arch")) {
12const SYS_tkill = 200;12 x86_64 => 9,
13const SYS_tgkill = 234;13 i386 => 90,
14const SYS_getrandom = 318;14 else => unreachable{},
15};
16const SYS_munmap = switch (@compile_var("arch")) {
17 x86_64 => 11,
18 i386 => 91,
19 else => unreachable{},
20};
21const SYS_rt_sigprocmask = switch (@compile_var("arch")) {
22 x86_64 => 14,
23 i386 => 175,
24 else => unreachable{},
25};
26const SYS_exit = switch (@compile_var("arch")) {
27 x86_64 => 60,
28 i386 => 1,
29 else => unreachable{},
30};
31const SYS_kill = switch (@compile_var("arch")) {
32 x86_64 => 62,
33 i386 => 37,
34 else => unreachable{},
35};
36const SYS_getgid = switch (@compile_var("arch")) {
37 x86_64 => 104,
38 i386 => 47,
39 else => unreachable{},
40};
41const SYS_gettid = switch (@compile_var("arch")) {
42 x86_64 => 186,
43 i386 => 224,
44 else => unreachable{},
45};
46const SYS_tkill = switch (@compile_var("arch")) {
47 x86_64 => 200,
48 i386 => 238,
49 else => unreachable{},
50};
51const SYS_tgkill = switch (@compile_var("arch")) {
52 x86_64 => 234,
53 i386 => 270,
54 else => unreachable{},
55};
56const SYS_getrandom = switch (@compile_var("arch")) {
57 x86_64 => 318,
58 i386 => 355,
59 else => unreachable{},
60};
1561
16pub const MMAP_PROT_NONE = 0;62pub const MMAP_PROT_NONE = 0;
17pub const MMAP_PROT_READ = 1;63pub const MMAP_PROT_READ = 1;
...@@ -63,14 +109,45 @@ const SIG_BLOCK = 0;...@@ -63,14 +109,45 @@ const SIG_BLOCK = 0;
63const SIG_UNBLOCK = 1;109const SIG_UNBLOCK = 1;
64const SIG_SETMASK = 2;110const SIG_SETMASK = 2;
65111
66fn syscall0(number: isize) -> isize {112const syscall0 = switch (@compile_var("arch")) {
113 x86_64 => x86_64_syscall0,
114 i386 => i386_syscall0,
115 else => unreachable{},
116};
117const syscall1 = switch (@compile_var("arch")) {
118 x86_64 => x86_64_syscall1,
119 i386 => i386_syscall1,
120 else => unreachable{},
121};
122const syscall2 = switch (@compile_var("arch")) {
123 x86_64 => x86_64_syscall2,
124 i386 => i386_syscall2,
125 else => unreachable{},
126};
127const syscall3 = switch (@compile_var("arch")) {
128 x86_64 => x86_64_syscall3,
129 i386 => i386_syscall3,
130 else => unreachable{},
131};
132const syscall4 = switch (@compile_var("arch")) {
133 x86_64 => x86_64_syscall4,
134 i386 => i386_syscall4,
135 else => unreachable{},
136};
137const syscall6 = switch (@compile_var("arch")) {
138 x86_64 => x86_64_syscall6,
139 i386 => i386_syscall6,
140 else => unreachable{},
141};
142
143fn x86_64_syscall0(number: isize) -> isize {
67 asm volatile ("syscall"144 asm volatile ("syscall"
68 : [ret] "={rax}" (-> isize)145 : [ret] "={rax}" (-> isize)
69 : [number] "{rax}" (number)146 : [number] "{rax}" (number)
70 : "rcx", "r11")147 : "rcx", "r11")
71}148}
72149
73fn syscall1(number: isize, arg1: isize) -> isize {150fn x86_64_syscall1(number: isize, arg1: isize) -> isize {
74 asm volatile ("syscall"151 asm volatile ("syscall"
75 : [ret] "={rax}" (-> isize)152 : [ret] "={rax}" (-> isize)
76 : [number] "{rax}" (number),153 : [number] "{rax}" (number),
...@@ -78,7 +155,7 @@ fn syscall1(number: isize, arg1: isize) -> isize {...@@ -78,7 +155,7 @@ fn syscall1(number: isize, arg1: isize) -> isize {
78 : "rcx", "r11")155 : "rcx", "r11")
79}156}
80157
81fn syscall2(number: isize, arg1: isize, arg2: isize) -> isize {158fn x86_64_syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
82 asm volatile ("syscall"159 asm volatile ("syscall"
83 : [ret] "={rax}" (-> isize)160 : [ret] "={rax}" (-> isize)
84 : [number] "{rax}" (number),161 : [number] "{rax}" (number),
...@@ -87,7 +164,7 @@ fn syscall2(number: isize, arg1: isize, arg2: isize) -> isize {...@@ -87,7 +164,7 @@ fn syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
87 : "rcx", "r11")164 : "rcx", "r11")
88}165}
89166
90fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {167fn x86_64_syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
91 asm volatile ("syscall"168 asm volatile ("syscall"
92 : [ret] "={rax}" (-> isize)169 : [ret] "={rax}" (-> isize)
93 : [number] "{rax}" (number),170 : [number] "{rax}" (number),
...@@ -97,7 +174,7 @@ fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {...@@ -97,7 +174,7 @@ fn syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
97 : "rcx", "r11")174 : "rcx", "r11")
98}175}
99176
100fn syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {177fn x86_64_syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
101 asm volatile ("syscall"178 asm volatile ("syscall"
102 : [ret] "={rax}" (-> isize)179 : [ret] "={rax}" (-> isize)
103 : [number] "{rax}" (number),180 : [number] "{rax}" (number),
...@@ -108,7 +185,7 @@ fn syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -...@@ -108,7 +185,7 @@ fn syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -
108 : "rcx", "r11")185 : "rcx", "r11")
109}186}
110187
111fn syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize, arg6: isize) -> isize {188fn x86_64_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize, arg6: isize) -> isize {
112 asm volatile ("syscall"189 asm volatile ("syscall"
113 : [ret] "={rax}" (-> isize)190 : [ret] "={rax}" (-> isize)
114 : [number] "{rax}" (number),191 : [number] "{rax}" (number),
...@@ -121,6 +198,58 @@ fn syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, a...@@ -121,6 +198,58 @@ fn syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, a
121 : "rcx", "r11")198 : "rcx", "r11")
122}199}
123200
201fn i386_syscall0(number: isize) -> isize {
202 asm volatile ("int $0x80"
203 : [ret] "={eax}" (-> isize)
204 : [number] "{eax}" (number))
205}
206
207fn i386_syscall1(number: isize, arg1: isize) -> isize {
208 asm volatile ("int $0x80"
209 : [ret] "={eax}" (-> isize)
210 : [number] "{eax}" (number),
211 [arg1] "{ebx}" (arg1))
212}
213
214fn i386_syscall2(number: isize, arg1: isize, arg2: isize) -> isize {
215 asm volatile ("int $0x80"
216 : [ret] "={eax}" (-> isize)
217 : [number] "{eax}" (number),
218 [arg1] "{ebx}" (arg1),
219 [arg2] "{ecx}" (arg2))
220}
221
222fn i386_syscall3(number: isize, arg1: isize, arg2: isize, arg3: isize) -> isize {
223 asm volatile ("int $0x80"
224 : [ret] "={eax}" (-> isize)
225 : [number] "{eax}" (number),
226 [arg1] "{ebx}" (arg1),
227 [arg2] "{ecx}" (arg2),
228 [arg3] "{edx}" (arg3))
229}
230
231fn i386_syscall4(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize) -> isize {
232 asm volatile ("int $0x80"
233 : [ret] "={eax}" (-> isize)
234 : [number] "{eax}" (number),
235 [arg1] "{ebx}" (arg1),
236 [arg2] "{ecx}" (arg2),
237 [arg3] "{edx}" (arg3),
238 [arg4] "{esi}" (arg4))
239}
240
241fn i386_syscall6(number: isize, arg1: isize, arg2: isize, arg3: isize, arg4: isize, arg5: isize, arg6: isize) -> isize {
242 asm volatile ("int $0x80"
243 : [ret] "={eax}" (-> isize)
244 : [number] "{eax}" (number),
245 [arg1] "{ebx}" (arg1),
246 [arg2] "{ecx}" (arg2),
247 [arg3] "{edx}" (arg3),
248 [arg4] "{esi}" (arg4),
249 [arg5] "{edi}" (arg5),
250 [arg6] "{ebp}" (arg6))
251}
252
124pub fn mmap(address: isize, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {253pub fn mmap(address: isize, length: isize, prot: isize, flags: isize, fd: isize, offset: isize) -> isize {
125 syscall6(SYS_mmap, address, length, prot, flags, fd, offset)254 syscall6(SYS_mmap, address, length, prot, flags, fd, offset)
126}255}
test/self_hosted.zig+6-1
...@@ -164,7 +164,12 @@ fn enum_type() {...@@ -164,7 +164,12 @@ fn enum_type() {
164 assert(bar == EnumTypeBar.B);164 assert(bar == EnumTypeBar.B);
165 assert(@member_count(EnumTypeFoo) == 3);165 assert(@member_count(EnumTypeFoo) == 3);
166 assert(@member_count(EnumTypeBar) == 4);166 assert(@member_count(EnumTypeBar) == 4);
167 assert(@sizeof(EnumTypeFoo) == 24);167 const expected_foo_size = switch (@compile_var("arch")) {
168 i386 => 20,
169 x86_64 => 24,
170 else => unreachable{},
171 };
172 assert(@sizeof(EnumTypeFoo) == expected_foo_size);
168 assert(@sizeof(EnumTypeBar) == 1);173 assert(@sizeof(EnumTypeBar) == 1);
169}174}
170struct EnumType {175struct EnumType {