| 1 | // Copyright 2020 Google, Inc. All Rights Reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package asm |
| 16 | |
| 17 | import ( |
| 18 | "flag" |
| 19 | "go/types" |
| 20 | "log" |
| 21 | "os" |
| 22 | |
| 23 | "github.com/mmcloughlin/avo/build" |
| 24 | "github.com/mmcloughlin/avo/buildtags" |
| 25 | "github.com/mmcloughlin/avo/gotypes" |
| 26 | "github.com/mmcloughlin/avo/ir" |
| 27 | ) |
| 28 | |
| 29 | // CLI utilities for ASM generator |
| 30 | |
| 31 | // AvoContext is an extended context for Avo that conveys information about |
| 32 | // the target architecture. In particular this allows some level of |
| 33 | // abstraction over registers. |
| 34 | type AvoContext struct { |
| 35 | *build.Context |
| 36 | Arch string |
| 37 | } |
| 38 | |
| 39 | // Missing instructions from avo. |
| 40 | |
| 41 | // INL is the INL instruction. |
| 42 | func (ctx *AvoContext) INL() { |
| 43 | ctx.Instruction(&ir.Instruction{Opcode: "INL"}) |
| 44 | } |
| 45 | |
| 46 | // INSB is the INSB instruction. |
| 47 | func (ctx *AvoContext) INSB() { |
| 48 | ctx.Instruction(&ir.Instruction{Opcode: "INSB"}) |
| 49 | } |
| 50 | |
| 51 | // OUTSB is the OUTSB instruction. |
| 52 | func (ctx *AvoContext) OUTSB() { |
| 53 | ctx.Instruction(&ir.Instruction{Opcode: "OUTSB"}) |
| 54 | } |
| 55 | |
| 56 | // REP is the REP instruction. |
| 57 | func (ctx *AvoContext) REP() { |
| 58 | ctx.Instruction(&ir.Instruction{Opcode: "REP"}) |
| 59 | } |
| 60 | |
| 61 | // AvoMainFunc is the type of functions that can be used with GenAsm. |
| 62 | type AvoMainFunc func(ctx *AvoContext) error |
| 63 | |
| 64 | // GenAsm is our main entry point for ASM code generation. |
| 65 | // It augments avo flags with a "-arch", the content of which is |
| 66 | // exposed in AvoContext. That value also drives the alignments in |
| 67 | // the generated code. |
| 68 | func GenAsm(f AvoMainFunc) { |
| 69 | var arch string |
| 70 | |
| 71 | // reset the flagset to allow NewFlags to work. |
| 72 | flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError) |
| 73 | var flags = build.NewFlags(flag.CommandLine) |
| 74 | flag.CommandLine.StringVar(&arch, "arch", "amd64", "arch to use") |
| 75 | |
| 76 | flag.Parse() |
| 77 | |
| 78 | // This is needed to get proper alignment of the return values. |
| 79 | // amd64 is the global default in avo, but not modifying it might |
| 80 | // result in misalignment of the stack. |
| 81 | gotypes.Sizes = types.SizesFor("gc", arch) |
| 82 | |
| 83 | cfg := flags.Config() |
| 84 | |
| 85 | ctx := &AvoContext{ |
| 86 | Context: build.NewContext(), |
| 87 | Arch: arch, |
| 88 | } |
| 89 | |
| 90 | ctx.Constraint(buildtags.Opt("gc")) |
| 91 | |
| 92 | if err := f(ctx); err != nil { |
| 93 | log.Fatal(err) |
| 94 | } |
| 95 | |
| 96 | os.Exit(build.Main(cfg, ctx.Context)) |
| 97 | } |