1 module sdc.sdc;
2 
3 final class SDC {
4 	import source.context;
5 	Context context;
6 
7 	import d.semantic.semantic;
8 	SemanticPass semantic;
9 
10 	import d.llvm.backend;
11 	LLVMBackend backend;
12 
13 	import d.ir.symbol;
14 	Module[] modules;
15 
16 	import sdc.config;
17 	this(Context context, string name, Config config) {
18 		this.context = context;
19 
20 		import std.algorithm, std.array, std.conv, std.path;
21 		auto includePaths = config.includePaths.map!(
22 				p => expandTilde(p).asAbsolutePath.asNormalizedPath.to!string())
23 			.array();
24 
25 		LLVMBackend evBackend;
26 
27 		import d.semantic.evaluator;
28 		Evaluator evb(SemanticPass pass) {
29 			if (evBackend is null) {
30 				evBackend = new LLVMBackend(pass, name, config.optLevel,
31 				                            config.linkerPaths);
32 			}
33 
34 			return evBackend.getEvaluator();
35 		}
36 
37 		import d.semantic.datalayout, d.object;
38 		DataLayout dlb(ObjectReference) {
39 			assert(evBackend !is null);
40 			return evBackend.getDataLayout();
41 		}
42 
43 		semantic = new SemanticPass(context, includePaths,
44 		                            config.enableUnittest, &evb, &dlb);
45 		backend = new LLVMBackend(semantic, name, config.optLevel,
46 		                          config.linkerPaths);
47 	}
48 
49 	void compile(string filename) {
50 		modules ~= semantic.add(filename);
51 	}
52 
53 	void buildMain() {
54 		semantic.terminate();
55 		backend.visit(semantic.buildMain(modules[0]));
56 	}
57 
58 	void outputLLVMAsm(string filename) {
59 		semantic.terminate();
60 		backend.emitLLVMAsm(modules, filename);
61 	}
62 
63 	void outputLLVMBitcode(string filename) {
64 		semantic.terminate();
65 		backend.emitLLVMBitcode(modules, filename);
66 	}
67 
68 	void outputAsm(string filename) {
69 		semantic.terminate();
70 		backend.emitAsm(modules, filename);
71 	}
72 
73 	void outputObj(string objFile) {
74 		semantic.terminate();
75 		backend.emitObject(modules, objFile);
76 	}
77 
78 	void linkExecutable(string objFile, string executable) {
79 		backend.link(objFile, executable);
80 	}
81 
82 	auto runUnittests() {
83 		semantic.terminate();
84 		return backend.runUnittests(modules);
85 	}
86 }