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

dynasm: Switch from C-based assembler to Lua-based assembler #575

Merged
merged 2 commits into from
Aug 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ PFLUASRC = $(shell cd ../deps/pflua/src && \
find . -regex '[^\#]*\.lua' -printf '%P ')
CSRC = $(shell find . -regex '[^\#]*\.c' -not -regex './arch/.*' -printf '%P ')
CHDR = $(shell find . -regex '[^\#]*\.h' -printf '%P ')
ASM = $(shell find . -regex '[^\#]*\.dasc' -printf '%P ')
ASM = $(shell find . -regex '[^\#]*\.dasl' -printf '%P ')
ARCHSRC= $(shell find . -regex '^./arch/[^\#]*\.c' -printf '%P ')
RMSRC = $(shell find . -name README.md.src -printf '%P ')
# regexp is to include program/foo but not program/foo/bar
Expand All @@ -32,7 +32,7 @@ PFLUAOBJ := $(patsubst %.lua,obj/%_lua.o,$(PFLUASRC))
COBJ := $(patsubst %.c,obj/%_c.o, $(CSRC))
HOBJ := $(patsubst %.h,obj/%_h.o, $(CHDR))
ARCHOBJ:= $(patsubst %.c,obj/%_c.o, $(ARCHSRC))
ASMOBJ := $(patsubst %.dasc,obj/%_dasc.o, $(ASM))
ASMOBJ := $(patsubst %.dasl,obj/%_dasl.o, $(ASM))
JITOBJS:= $(patsubst %,obj/jit_%.o,$(JITSRC))
EXTRAOBJS := obj/jit_tprof.o obj/jit_vmprof.o obj/strict.o
RMOBJS := $(patsubst %.src,%,$(RMSRC))
Expand Down Expand Up @@ -168,10 +168,10 @@ $(HOBJ): obj/%_h.o: %.h Makefile | $(OBJDIR)
echo "]=============]") > $(basename $@).luah
$(Q) luajit -bg -n $(subst /,.,$*)_h $(basename $@).luah $@

$(ASMOBJ): obj/%_dasc.o: %.dasc $(CHDR) Makefile | $(OBJDIR)
$(ASMOBJ): obj/%_dasl.o: %.dasl $(CHDR) Makefile | $(OBJDIR)
$(E) "ASM $@"
$(Q) luajit ../deps/luajit/dynasm/dynasm.lua -o $@.gen $<
$(Q) gcc $(DEBUG) -Wl,-E -I ../deps/luajit/src -I . -I ../deps/luajit -c -Wall -Werror -x c -o $@ $@.gen
$(Q) luajit dynasm.lua -o $@.gen $<
$(Q) luajit -bg -n $(subst /,.,$*) $@.gen $@

$(JITOBJS): obj/jit_%.o: ../deps/luajit/src/jit/%.lua $(OBJDIR)
$(E) "LUA $@"
Expand Down
57 changes: 0 additions & 57 deletions src/apps/example/asm.dasc

This file was deleted.

62 changes: 62 additions & 0 deletions src/apps/example/asm.dasl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-- Example app written in x86-64 assembly language.
module(...,package.seeall)

local ffi = require("ffi")
local C = ffi.C

local dasm = require("dasm")

--
-- Compile machine code
--
-- This is a really simple example. We are going to assigned 'fptr' to
-- a machine code function that simply stores a magic number in the
-- 'asm_status' struct so that we know it has executed.

local fptr
local asm_status = ffi.new("uint32_t[1]")

|.arch x64
|.actionlist actions
local Dst = dasm.new(actions)
| mov dword [asm_status], 0xdeadbeef
| ret
code = Dst:build() -- assign to 'code' to avoid machine code being GC'd
fptr = ffi.cast("void(*)()", code)

--
-- Lua app
--

local config = require("core.config")
local app = require("core.app")
local basic_apps = require("apps.basic.basic_apps")

Asm = {}

function Asm:new ()
return setmetatable({}, {__index=Asm})
end

function Asm:pull ()
end

function Asm:push ()
-- Call generated code.
fptr()
end

function selftest ()
print("selftest: asm")
local c = config.new()
config.app(c, "source", basic_apps.Source)
config.app(c, "sink", basic_apps.Sink)
config.app(c, "asm", Asm)
config.link(c, "source.tx -> asm.rx")
config.link(c, "asm.tx -> sink.rx")
app.configure(c)
app.main({duration = 0.1})
print("magic number: 0x"..bit.tohex(asm_status[0]))
assert(asm_status[0] == 0xdeadbeef, "bad magic")
print("selftest: ok")
end
46 changes: 0 additions & 46 deletions src/apps/example/asm.lua

This file was deleted.

Loading