This commit is contained in:
parent
e6a6be1181
commit
7e9f5d500f
10 changed files with 19 additions and 18 deletions
|
@ -17,7 +17,7 @@ func _build(args []string) int {
|
||||||
return exit(err)
|
return exit(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := compiler.Compile(b)
|
env, err := compiler.Compile(b)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return exit(err)
|
return exit(err)
|
||||||
|
@ -27,7 +27,7 @@ func _build(args []string) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
err = linker.WriteFile(b.Executable(), b, result)
|
err = linker.WriteFile(b.Executable(), env)
|
||||||
return exit(err)
|
return exit(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,7 +29,7 @@ func run(args []string) int {
|
||||||
return exit(err)
|
return exit(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
linker.Write(file, b, env)
|
linker.Write(file, env)
|
||||||
err = memfile.Exec(file)
|
err = memfile.Exec(file)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"git.urbach.dev/cli/q/src/build"
|
||||||
"git.urbach.dev/cli/q/src/fs"
|
"git.urbach.dev/cli/q/src/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Environment holds information about the entire build.
|
// Environment holds information about the entire build.
|
||||||
type Environment struct {
|
type Environment struct {
|
||||||
|
Build *build.Build
|
||||||
Functions map[string]*Function
|
Functions map[string]*Function
|
||||||
Files []*fs.File
|
Files []*fs.File
|
||||||
}
|
}
|
|
@ -32,9 +32,8 @@ type Function struct {
|
||||||
// NewFunction creates a new function.
|
// NewFunction creates a new function.
|
||||||
func NewFunction(name string, pkg string, file *fs.File) *Function {
|
func NewFunction(name string, pkg string, file *fs.File) *Function {
|
||||||
return &Function{
|
return &Function{
|
||||||
Name: name,
|
Name: name,
|
||||||
Package: pkg,
|
Package: pkg,
|
||||||
|
|
||||||
File: file,
|
File: file,
|
||||||
Identifiers: make(map[string]ssa.Value, 8),
|
Identifiers: make(map[string]ssa.Value, 8),
|
||||||
IR: ssa.IR{
|
IR: ssa.IR{
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Write writes an executable to the given writer.
|
// Write writes an executable to the given writer.
|
||||||
func Write(writer io.WriteSeeker, b *build.Build, env *core.Environment) {
|
func Write(writer io.WriteSeeker, env *core.Environment) {
|
||||||
program := asm.Assembler{
|
program := asm.Assembler{
|
||||||
Instructions: make([]asm.Instruction, 0, 32),
|
Instructions: make([]asm.Instruction, 0, 32),
|
||||||
Data: make(data.Data, 32),
|
Data: make(data.Data, 32),
|
||||||
|
@ -28,14 +28,14 @@ func Write(writer io.WriteSeeker, b *build.Build, env *core.Environment) {
|
||||||
program.Merge(&f.Assembler)
|
program.Merge(&f.Assembler)
|
||||||
})
|
})
|
||||||
|
|
||||||
code, data, libs := program.Compile(b)
|
code, data, libs := program.Compile(env.Build)
|
||||||
|
|
||||||
switch b.OS {
|
switch env.Build.OS {
|
||||||
case build.Linux:
|
case build.Linux:
|
||||||
elf.Write(writer, b, code, data)
|
elf.Write(writer, env.Build, code, data)
|
||||||
case build.Mac:
|
case build.Mac:
|
||||||
macho.Write(writer, b, code, data)
|
macho.Write(writer, env.Build, code, data)
|
||||||
case build.Windows:
|
case build.Windows:
|
||||||
pe.Write(writer, b, code, data, libs)
|
pe.Write(writer, env.Build, code, data, libs)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -3,19 +3,18 @@ package linker
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
"git.urbach.dev/cli/q/src/build"
|
|
||||||
"git.urbach.dev/cli/q/src/core"
|
"git.urbach.dev/cli/q/src/core"
|
||||||
)
|
)
|
||||||
|
|
||||||
// WriteFile writes an executable file to disk.
|
// WriteFile writes an executable file to disk.
|
||||||
func WriteFile(executable string, b *build.Build, env *core.Environment) error {
|
func WriteFile(executable string, env *core.Environment) error {
|
||||||
file, err := os.Create(executable)
|
file, err := os.Create(executable)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
Write(file, b, env)
|
Write(file, env)
|
||||||
err = file.Chmod(0o755)
|
err = file.Chmod(0o755)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -18,6 +18,6 @@ func TestWrite(t *testing.T) {
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
writer := &exe.Discard{}
|
writer := &exe.Discard{}
|
||||||
linker.Write(writer, b, env)
|
linker.Write(writer, env)
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -33,7 +33,7 @@ func TestHelloExample(t *testing.T) {
|
||||||
env, err := compiler.Compile(b)
|
env, err := compiler.Compile(b)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
linker.Write(file, b, env)
|
linker.Write(file, env)
|
||||||
err = memfile.Exec(file)
|
err = memfile.Exec(file)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
|
|
|
@ -28,6 +28,7 @@ func Scan(b *build.Build) (*core.Environment, error) {
|
||||||
}()
|
}()
|
||||||
|
|
||||||
all := &core.Environment{
|
all := &core.Environment{
|
||||||
|
Build: b,
|
||||||
Files: make([]*fs.File, 0, 8),
|
Files: make([]*fs.File, 0, 8),
|
||||||
Functions: make(map[string]*core.Function, 32),
|
Functions: make(map[string]*core.Function, 32),
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ func (test *testRun) Run(t *testing.T, path string) {
|
||||||
|
|
||||||
executable := b.Executable()
|
executable := b.Executable()
|
||||||
executable = filepath.Join(tmpDir, filepath.Base(executable))
|
executable = filepath.Join(tmpDir, filepath.Base(executable))
|
||||||
err = linker.WriteFile(executable, b, env)
|
err = linker.WriteFile(executable, env)
|
||||||
assert.Nil(t, err)
|
assert.Nil(t, err)
|
||||||
|
|
||||||
stat, err := os.Stat(executable)
|
stat, err := os.Stat(executable)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue