Skip to content

Commit 3e9d21b

Browse files
committed
Separate x86 and x86_64
1 parent e6bdd73 commit 3e9d21b

File tree

173 files changed

+1848
-198
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

173 files changed

+1848
-198
lines changed

.gitignore

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
wasmpy.egg-info
2-
wasmpy/x86/**/*
3-
!wasmpy/*/lib/*
4-
!wasmpy/*/lib/
5-
!wasmpy/**/*.s
2+
wasmpy/arch/*/*
3+
!wasmpy/arch/*
4+
!wasmpy/arch/*/*.s
5+
!wasmpy/arch/*/lib/
66
**/opcodes.cpp
77
__pycache__
88
.vscode

MANIFEST.in

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
exclude *.cpp
22
include LICENSE
3-
graft wasmpy/x86
3+
graft wasmpy/arch
44
recursive-exclude wasmpy *.o
55
recursive-exclude wasmpy *.cpp
6-
recursive-include wasmpy *.hpp
76
recursive-exclude wasmpy *.tmp
7+
recursive-include wasmpy *.hpp
88
recursive-include wasmpy lib.cpp

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ stack: python 3.7
1212
init:
1313
- cmd: set PATH=C:\Python37;C:\Python37\Scripts;C:\msys64\mingw64\bin;%PATH%
1414

15-
install: python -m pip install cibuildwheel==2.13.1 && python setup.py assemble
15+
install: python -m pip install cibuildwheel==2.13.1 && python setup.py assemble --targets=x86,x86_64
1616

1717
build_script:
1818
- ps: Invoke-Expression $env:BUILD_SCRIPT

setup.py

Lines changed: 82 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -183,127 +183,71 @@
183183
}
184184

185185

186-
def listdir(path):
186+
def listdir(path: str) -> list:
187187
return [os.path.join(path, p) for p in os.listdir(path)]
188188

189189

190-
class assemble(setuptools.Command):
191-
user_options = []
192-
193-
def initialize_options(self):
194-
pass
190+
def is_x86() -> bool:
191+
return platform.machine() in ("x86", "i386", "i686", "AMD64", "x86_64")
195192

196-
def finalize_options(self):
197-
pass
198193

199-
def run(self):
200-
for source in listdir("wasmpy/x86") + listdir("wasmpy/x86/64"):
201-
if os.path.isdir(source):
202-
continue
194+
class assemble(setuptools.Command):
195+
user_options = [("targets=", "T", None)]
203196

204-
# assemble x86 instructions
205-
if os.path.splitext(source)[1].lower() == ".s":
206-
subprocess.call(
207-
[
208-
"as",
209-
source,
210-
"-o",
211-
os.path.splitext(source)[0] + ".o",
212-
]
213-
)
197+
def initialize_options(self):
198+
if is_x86():
199+
if struct.calcsize("P") == 4:
200+
self.targets = "x86"
214201

215-
if platform.system() == "Linux":
216-
subprocess.call(
217-
[
218-
"ld",
219-
"--oformat",
220-
"binary",
221-
os.path.splitext(source)[0] + ".o",
222-
"-o",
223-
os.path.splitext(source)[0],
224-
]
225-
)
202+
else:
203+
self.targets = "x86_64"
226204

227-
elif platform.system() == "Windows":
228-
subprocess.call(
229-
[
230-
"ld",
231-
"-T",
232-
"NUL",
233-
"--image-base",
234-
"0",
235-
os.path.splitext(source)[0] + ".o",
236-
"-o",
237-
os.path.splitext(source)[0] + ".tmp",
238-
]
239-
)
240-
subprocess.call(
241-
[
242-
"objcopy",
243-
"-O",
244-
"binary",
245-
"-j",
246-
".text",
247-
os.path.splitext(source)[0] + ".tmp",
248-
os.path.splitext(source)[0],
249-
]
250-
)
205+
else:
206+
raise ValueError("Unknown architecture")
251207

252-
for source in listdir("wasmpy/x86/32"):
253-
if os.path.isdir(source):
254-
continue
208+
def finalize_options(self):
209+
self.targets = self.targets.split(",")
210+
for target in self.targets:
211+
if target not in os.listdir("wasmpy/arch") or os.path.isfile(
212+
f"wasmpy/{target}"
213+
):
214+
raise ValueError(f"Unknown architecture: {target}")
255215

256-
# assemble x86 32 bit specific instructions
257-
if os.path.splitext(source)[1].lower() == ".s":
258-
subprocess.call(
259-
[
260-
"as",
261-
"--32",
262-
source,
263-
"-o",
264-
os.path.splitext(source)[0] + ".o",
265-
]
266-
)
216+
def run(self):
217+
args = {
218+
"x86_64": [
219+
["as", "-o"],
220+
["ld", "--oformat", "binary", "-o"],
221+
["ld", "-T", "NUL", "--image-base", "0", "-o"],
222+
["objcopy", "-O", "binary", "-j", ".text"],
223+
]
224+
}
225+
226+
args["x86"] = [i.copy() for i in args["x86_64"]]
227+
args["x86"][0].insert(1, "--32")
228+
args["x86"][1].insert(1, "-melf_i386")
229+
args["x86"][2].insert(1, "-mi386pe")
230+
231+
for target in self.targets:
232+
arg = args[target]
233+
for source in listdir(f"wasmpy/arch/{target}"):
234+
if os.path.isdir(source):
235+
continue
267236

268-
if platform.system() == "Linux":
269-
subprocess.call(
270-
[
271-
"ld",
272-
"-melf_i386",
273-
"--oformat",
274-
"binary",
275-
os.path.splitext(source)[0] + ".o",
276-
"-o",
277-
os.path.splitext(source)[0],
278-
]
279-
)
237+
if os.path.splitext(source)[1].lower() == ".s":
238+
name = os.path.splitext(source)[0]
239+
cmd = arg[0] + [name + ".o", source]
240+
subprocess.call(cmd)
280241

281-
elif platform.system() == "Windows":
282-
subprocess.call(
283-
[
284-
"ld",
285-
"-mi386pe",
286-
"-T",
287-
"NUL",
288-
"--image-base",
289-
"0",
290-
os.path.splitext(source)[0] + ".o",
291-
"-o",
292-
os.path.splitext(source)[0] + ".tmp",
293-
]
294-
)
242+
if platform.system() == "Linux":
243+
cmd = arg[1] + [name, name + ".o"]
244+
subprocess.call(cmd)
295245

296-
subprocess.call(
297-
[
298-
"objcopy",
299-
"-O",
300-
"binary",
301-
"-j",
302-
".text",
303-
os.path.splitext(source)[0] + ".tmp",
304-
os.path.splitext(source)[0],
305-
]
306-
)
246+
elif platform.system() == "Windows":
247+
cmd = arg[2] + [name + ".tmp", name + ".o"]
248+
subprocess.call(cmd)
249+
cmd = arg[3] + [name + ".tmp", name]
250+
subprocess.call(cmd)
307251

308252

309253
class tidy(setuptools.Command):
@@ -316,19 +260,19 @@ def finalize_options(self):
316260
pass
317261

318262
def run(self):
319-
if os.path.exists("wasmpy/x86/lib/opcodes.cpp"):
320-
os.remove("wasmpy/x86/lib/opcodes.cpp")
321-
322-
for file in (
323-
listdir("wasmpy/x86")
324-
+ listdir("wasmpy/x86/64")
325-
+ listdir("wasmpy/x86/32")
326-
):
327-
if os.path.isdir(file):
263+
for machine in os.listdir("wasmpy/arch"):
264+
if os.path.isfile("wasmpy/arch/" + machine):
328265
continue
329266

330-
if os.path.splitext(file)[1] != ".s":
331-
os.remove(file)
267+
if os.path.exists(f"wasmpy/arch/{machine}/lib/opcodes.cpp"):
268+
os.remove(f"wasmpy/arch/{machine}/lib/opcodes.cpp")
269+
270+
for file in listdir(f"wasmpy/arch/{machine}"):
271+
if os.path.isdir(file):
272+
continue
273+
274+
if os.path.splitext(file)[1] != ".s":
275+
os.remove(file)
332276

333277

334278
class gen_opcodes(setuptools.Command):
@@ -347,7 +291,6 @@ def run(self):
347291
os.path.join(os.path.dirname(__file__), "wasmpy", "opcodes.json")
348292
) as fp:
349293
data = json.load(fp)
350-
consumes = data["consumes"]
351294
for group in data["opcodes"]:
352295
opcodes.update(
353296
dict(
@@ -361,9 +304,13 @@ def run(self):
361304
)
362305
)
363306

364-
with open("wasmpy/x86/lib/opcodes.cpp", "w+") as out:
365-
bits = struct.calcsize("P")
307+
if struct.calcsize("P") == 4:
308+
machine = "x86"
309+
310+
else:
311+
machine = "x86_64"
366312

313+
with open(f"wasmpy/arch/{machine}/lib/opcodes.cpp", "w+") as out:
367314
out.writelines(
368315
(
369316
"// auto-generated\n\n",
@@ -376,13 +323,7 @@ def run(self):
376323
)
377324
)
378325

379-
if bits == 8:
380-
extra = listdir("wasmpy/x86/64")
381-
382-
elif bits == 4:
383-
extra = listdir("wasmpy/x86/32")
384-
385-
for file in listdir("wasmpy/x86") + extra:
326+
for file in listdir(f"wasmpy/arch/{machine}"):
386327
if os.path.isdir(file):
387328
continue
388329

@@ -407,7 +348,7 @@ def run(self):
407348

408349
out.write("default:\n\t\tbreak;\n\t}\n\treturn insts;\n}\n\n")
409350

410-
for file in listdir("wasmpy/x86") + extra:
351+
for file in listdir(f"wasmpy/arch/{machine}"):
411352
if os.path.isdir(file):
412353
continue
413354

@@ -451,11 +392,20 @@ def run(self):
451392
),
452393
]
453394

454-
if platform.machine() in ("x86", "i386", "i686", "AMD64", "x86_64"):
395+
if is_x86():
396+
if struct.calcsize("P") == 4:
397+
machine = "x86"
398+
399+
else:
400+
machine = "x86_64"
401+
455402
ext.append(
456403
setuptools.Extension(
457404
"wasmpy.nativelib",
458-
sources=["wasmpy/x86/lib/lib.cpp", "wasmpy/x86/lib/opcodes.cpp"],
405+
sources=[
406+
f"wasmpy/arch/{machine}/lib/lib.cpp",
407+
f"wasmpy/arch/{machine}/lib/opcodes.cpp",
408+
],
459409
py_limited_api=True,
460410
)
461411
)
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)