Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Woot? Pluggable backends! That's a first shot - now to make 'plugins'…

… more discoverable, actually have useful buildfiles and maybe platform-abstract a lil' ?
  • Loading branch information...
commit f233f7c48610ccaa9620f025438d933c0b207bf4 1 parent 962ecb7
nddrylliog authored
View
1  .gitignore
@@ -2,6 +2,7 @@
.libs
bin/*
build/*
+plugins/*
core
callgrind.out.*
a.out
View
0  source/backend/c89/C89Ast.ooc → backends/backend/c89/Ast.ooc
File renamed without changes
View
0  source/backend/c89/AstTest.ooc → backends/backend/c89/AstTest.ooc
File renamed without changes
View
4 source/backend/c89/C89Backend.ooc → backends/backend/c89/Backend.ooc
@@ -7,7 +7,7 @@ import structs/[HashMap, ArrayList, List]
import io/[File, FileWriter]
import frontend/BuildParams
-import C89Ast, StackGenerator, ../Backend
+import Ast, StackGenerator, ../Backend
CallBack: class {
f: Func (Node) -> Object
@@ -15,7 +15,7 @@ CallBack: class {
init: func (=f) {}
}
-C89Backend: class extends Backend {
+c89_Backend: class extends Backend {
process: func (module: Module, params: BuildParams) {
C89Generator new(module, params)
View
2  source/backend/c89/StackGenerator.ooc → backends/backend/c89/StackGenerator.ooc
@@ -1,5 +1,5 @@
-import structs/Stack, C89Ast
+import structs/Stack, Ast
StackGenerator: abstract class {
View
0  source/backend/c89/headers/HeaderParser.ooc → ...ends/backend/c89/headers/HeaderParser.ooc
File renamed without changes
View
4 dl.use
@@ -0,0 +1,4 @@
+Name: libdl
+Description: Dynamic Loading library
+Libs: -ldl
+Includes: dlfcn.h
View
2  source/ast/FuncDecl.ooc
@@ -42,7 +42,6 @@ FuncDecl: class extends Expression {
task queue(retType)
resolved = true // artificial testing
- "Resolving body of %s" printfln(toString())
task queue(body)
autoReturn(task)
}
@@ -68,7 +67,6 @@ FuncDecl: class extends Expression {
}
resolveAccess: func (acc: Access, task: Task, suggest: Func (Var)) {
- "Resolving access %s in %s" printfln(acc toString(), toString())
v := args get(acc name)
if(v) suggest(v)
}
View
2  source/ast/Scope.ooc
@@ -18,7 +18,7 @@ Scope: class extends Node {
resolveAccess: func (acc: Access, task: Task, suggest: Func (Var)) {
idx : Int = -1
- "Looking for %s in %s" printfln(acc toString(), toString())
+ //"Looking for %s in %s" printfln(acc toString(), toString())
if(task has("noindex")) {
size := body size
View
71 source/backend/BackendFactory.ooc
@@ -1,16 +1,73 @@
-
import Backend
-
-import c89/C89Backend
+import io/File
BackendFactory: class {
+ previousHandle: static LibHandle = null
+
make: static func (name: String) -> Backend {
- // ideally we'd use dynamic library loading here to have pluggable backends :)
- match name {
- case "c89" => C89Backend new()
- case => "Unknown backend specified: '%s'" printfln(name); null
+ if(previousHandle) {
+ "Closing previous handle"
+ dlclose(previousHandle)
}
+
+ prefix := name + "_backend"
+
+ // TODO ohum: plugins is not necessarily in "./"
+ plugins := File new("plugins")
+
+ path := ""
+ plugins getChildren() each(|child|
+ if(child name() startsWith?(prefix)) {
+ "Found backend %s in %s" printfln(child path, name)
+ path = child path
+ } else {
+ "Ignoring %s" printfln(child path)
+ }
+ )
+
+ if(!path empty?()) {
+ handle := dlopen(path, RTLD_LAZY)
+
+ if(!handle) {
+ "Error while opening pluggable backend %s: %s" printfln(path, dlerror())
+ return null
+ }
+
+ // that's the constructor we're looking for!
+ symbolName := "backend_%s_Backend__%s_Backend_new" format(name, name)
+
+ "Looking for symbol %s" printfln(symbolName)
+ constructor := dlsym(handle, symbolName)
+
+ "Got address %p" printfln(constructor)
+ if(!constructor) {
+ "Symbol '%s' not found in backend %s" printfln(symbolName, path)
+ dlclose(handle)
+ return null
+ }
+
+ callable: Func -> Backend
+ callable as Closure thunk = constructor
+
+ backend := callable()
+ if(!backend) {
+ "Couldn't instantiate backend for '%s', please report this bug to backend maintainers" printfln(name)
+ }
+ "Got backend %s" printfln(backend class name)
+ return backend
+ }
+ null
}
}
+
+/* C binding part */
+use dl
+
+LibHandle: cover from Pointer {}
+RTLD_LAZY, RTLD_NOW, RTLD_GLOBAL: extern Int
+dlopen : extern func (fileName: CString, flag: Int) -> LibHandle
+dlsym : extern func (handle: LibHandle, symbolName: CString) -> Pointer
+dlclose : extern func (handle: LibHandle) -> Int
+dlerror : extern func -> CString
View
5 source/frontend/BuildParams.ooc
@@ -28,6 +28,7 @@ BuildParams: class {
verbose += 1
case "V" =>
"oc v%s - huhu" printfln(VERSION)
+ exit(0)
case "self" =>
self = val
case =>
@@ -38,6 +39,10 @@ BuildParams: class {
if(!backend) {
if(verbose > 0) "No backend selected, using C89 backend" println()
backend = BackendFactory make("c89")
+ if(!backend) {
+ "Couldn't load c89 backend, bailing out!" println()
+ exit(1)
+ }
}
if(verbose > 0) {
"oc v%s sourcepath = %s outpath = %s backend is %s" printfln(VERSION, sourcepath join(":"), outpath, backend class name)
View
10 source/main.ooc
@@ -8,27 +8,27 @@ main: func (args: ArrayList<String>) {
opts := Opts new(args)
+ params := BuildParams new(opts opts)
if(opts args empty?()) {
"Usage: oc file.ooc" println()
exit(1)
}
opts args each(|arg|
- compile(arg, opts)
+ compile(arg, params)
)
}
-compile: func (file: String, opts: Opts) {
-
- params := BuildParams new(opts opts)
+compile: func (file: String, params: BuildParams) {
+ // parse main module and dependencies!
pool := ParsingPool new()
mainJob := ParsingJob new(file, null)
pool push(mainJob)
pool exhaust()
+
mainJob module main? = true
-
Resolver new(params, mainJob module) start()
}
Please sign in to comment.
Something went wrong with that request. Please try again.