github.com/jspc/eggos@v0.5.1-0.20221028160421-556c75c878a5/kernel/genvector.go (about) 1 // +build ignore 2 3 package main 4 5 import ( 6 "bytes" 7 "fmt" 8 "io/ioutil" 9 "log" 10 ) 11 12 func hasErrCode(idx int) bool { 13 return idx == 8 || (idx >= 10 && idx <= 14) || idx == 17 14 } 15 16 func genasm() { 17 buf := new(bytes.Buffer) 18 buf.WriteString("// Code generated by genvector.go using 'go generate'. DO NOT EDIT.\n") 19 buf.WriteString(`#include "textflag.h"`) 20 buf.WriteString("\n\n") 21 for i := 0; i < 256; i++ { 22 fmt.Fprintf(buf, "TEXT ·trap%d(SB), NOSPLIT, $0-0\n", i) 23 if !hasErrCode(i) { 24 buf.WriteString(" PUSHQ $0\n") 25 } 26 fmt.Fprintf(buf, " PUSHQ $%d\n", i) 27 buf.WriteString(" JMP alltraps(SB)\n") 28 buf.WriteString(" POPQ AX\n") 29 if !hasErrCode(i) { 30 buf.WriteString(" POPQ AX\n") 31 } 32 buf.WriteString(" RET\n\n") 33 } 34 err := ioutil.WriteFile("vectors.s", buf.Bytes(), 0644) 35 if err != nil { 36 log.Fatal(err) 37 } 38 } 39 40 func gengo() { 41 buf := new(bytes.Buffer) 42 buf.WriteString("// Code generated by genvector.go using 'go generate'. DO NOT EDIT.\n") 43 buf.WriteString("package kernel\n\n") 44 for i := 0; i < 256; i++ { 45 fmt.Fprintf(buf, "//go:nosplit\n") 46 fmt.Fprintf(buf, "func trap%d()\n", i) 47 } 48 buf.WriteString("\n") 49 50 buf.WriteString("var vectors = [...]func(){\n") 51 for i := 0; i < 256; i++ { 52 fmt.Fprintf(buf, "trap%d,\n", i) 53 } 54 buf.WriteString("}\n\n") 55 56 err := ioutil.WriteFile("vectors.go", buf.Bytes(), 0644) 57 if err != nil { 58 log.Fatal(err) 59 } 60 } 61 62 func main() { 63 genasm() 64 gengo() 65 }