Files
avo/examples/add
Michael McLoughlin 4550badf58 doc: scripts to run embedmd on markdown files
This will help keep READMEs in sync with code.

Also adds a README for the add example.

Updates #14
2018-12-31 20:25:29 -08:00
..
2018-12-12 00:02:22 -08:00
2018-12-31 00:23:27 -08:00
2018-12-27 23:09:44 -08:00
2018-12-27 23:09:44 -08:00

add

Add two numbers with avo.

The code generator is as follows:

// +build ignore

package main

import (
	. "github.com/mmcloughlin/avo/build"
)

func main() {
	TEXT("Add", "func(x, y uint64) uint64")
	Doc("Add adds x and y.")
	x := Load(Param("x"), GP64v())
	y := Load(Param("y"), GP64v())
	ADDQ(x, y)
	Store(y, ReturnIndex(0))
	RET()
	Generate()
}

We use a go:generate line to produce the assembly and Go stub files together.

//go:generate go run asm.go -out add.s -stubs stub.go

This produces add.s as follows:

// Code generated by command: go run asm.go -out add.s -stubs stub.go. DO NOT EDIT.

// func Add(x uint64, y uint64) uint64
TEXT ·Add(SB), $0-24
	MOVQ	x(FP), AX
	MOVQ	y+8(FP), CX
	ADDQ	AX, CX
	MOVQ	CX, ret+16(FP)
	RET