github.com/icexin/eggos@v0.4.2-0.20220216025428-78b167e4f349/kernel/genvector.go (about)

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