-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathasm.go
executable file
·70 lines (63 loc) · 1.83 KB
/
asm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Package asm implements a parser for LLVM IR assembly.
package asm
import (
"io"
"io/ioutil"
"github.com/geode-lang/geode/llvm/asm/internal/ast"
"github.com/geode-lang/geode/llvm/asm/internal/irx"
"github.com/geode-lang/geode/llvm/asm/internal/lexer"
"github.com/geode-lang/geode/llvm/asm/internal/parser"
"github.com/geode-lang/geode/llvm/ir"
"github.com/pkg/errors"
)
// ParseFile parses the given LLVM IR assembly file into an LLVM IR module.
func ParseFile(path string) (*ir.Module, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return nil, errors.WithStack(err)
}
return ParseBytes(buf)
}
// Parse parses the given LLVM IR assembly file into an LLVM IR module, reading
// from r.
func Parse(r io.Reader) (*ir.Module, error) {
buf, err := ioutil.ReadAll(r)
if err != nil {
return nil, errors.WithStack(err)
}
return ParseBytes(buf)
}
// ParseBytes parses the given LLVM IR assembly file into an LLVM IR module,
// reading from b.
func ParseBytes(b []byte) (*ir.Module, error) {
module, err := parseBytes(b)
if err != nil {
return nil, errors.WithStack(err)
}
// Translate the AST of the module to an equivalent LLVM IR module.
m, err := irx.Translate(module)
if err != nil {
return nil, errors.WithStack(err)
}
return m, nil
}
// ParseString parses the given LLVM IR assembly file into an LLVM IR module,
// reading from s.
func ParseString(s string) (*ir.Module, error) {
return ParseBytes([]byte(s))
}
// parseBytes parses the given LLVM IR assembly file into an AST, reading from
// b.
func parseBytes(b []byte) (*ast.Module, error) {
l := lexer.NewLexer(b)
p := parser.NewParser()
module, err := p.Parse(l)
if err != nil {
return nil, errors.WithStack(err)
}
m, ok := module.(*ast.Module)
if !ok {
return nil, errors.Errorf("invalid module type; expected *ast.Module, got %T", module)
}
return m, nil
}