// runtime.def -- runtime functions called by generated code.  -*- C++ -*-

// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Definitions for the Go runtime functions.

// Parameter type helper macros.
#define ABFT6(T1, T2, T3, T4, T5, T6) \
  { RFT_ ## T1, RFT_ ## T2, RFT_ ## T3, RFT_ ## T4, RFT_ ## T5, RFT_ ## T6 }
#define P0()			ABFT6(VOID, VOID, VOID, VOID, VOID, VOID)
#define P1(T)			ABFT6(T, VOID, VOID, VOID, VOID, VOID)
#define P2(T1, T2)		ABFT6(T1, T2, VOID, VOID, VOID, VOID)
#define P3(T1, T2, T3)		ABFT6(T1, T2, T3, VOID, VOID, VOID)
#define P4(T1, T2, T3, T4)	ABFT6(T1, T2, T3, T4, VOID, VOID)
#define P5(T1, T2, T3, T4, T5)	ABFT6(T1, T2, T3, T4, T5, VOID)
#define P6(T1,T2,T3,T4,T5,T6)	ABFT6(T1, T2, T3, T4, T5, T6)

// Result type helper macros.
#define ABFT2(T1, T2) { RFT_ ## T1, RFT_ ## T2 }
#define R0()			ABFT2(VOID, VOID)
#define R1(T)			ABFT2(T, VOID)
#define R2(T1, T2)		ABFT2(T1, T2)

// Define all the Go runtime functions.  The first parameter is the
// enum code used to refer to the function.  The second parameter is
// the name.  The third is the parameter types and the fourth is the
// result types.

// The standard C memcmp function, used for struct comparisons.
DEF_GO_RUNTIME(MEMCMP, "__go_memcmp", P3(POINTER, POINTER, UINTPTR), R1(INT))

// Range over a string, returning the next index.
DEF_GO_RUNTIME(STRINGITER, "runtime.stringiter", P2(STRING, INT), R1(INT))

// Range over a string, returning the next index and character.
DEF_GO_RUNTIME(STRINGITER2, "runtime.stringiter2", P2(STRING, INT),
	       R2(INT, RUNE))

// Concatenate strings.
DEF_GO_RUNTIME(CONCATSTRINGS, "runtime.concatstrings", P2(POINTER, SLICE),
	       R1(STRING))
DEF_GO_RUNTIME(CONCATSTRING2, "runtime.concatstring2",
	       P2(POINTER, ARRAY2STRING), R1(STRING))
DEF_GO_RUNTIME(CONCATSTRING3, "runtime.concatstring3",
	       P2(POINTER, ARRAY3STRING), R1(STRING))
DEF_GO_RUNTIME(CONCATSTRING4, "runtime.concatstring4",
	       P2(POINTER, ARRAY4STRING), R1(STRING))
DEF_GO_RUNTIME(CONCATSTRING5, "runtime.concatstring5",
	       P2(POINTER, ARRAY5STRING), R1(STRING))

// Compare two strings.
DEF_GO_RUNTIME(STRCMP, "__go_strcmp", P2(STRING, STRING), R1(INT))

// Take a slice of a string.
DEF_GO_RUNTIME(STRING_SLICE, "__go_string_slice", P3(STRING, INT, INT),
	       R1(STRING))

// Convert an integer to a string.
DEF_GO_RUNTIME(INTSTRING, "runtime.intstring", P2(POINTER, INT64), R1(STRING))

// Convert a []byte to a string.
DEF_GO_RUNTIME(SLICEBYTETOSTRING, "runtime.slicebytetostring",
	       P2(POINTER, SLICE), R1(STRING))

// Convert a []rune to a string.
DEF_GO_RUNTIME(SLICERUNETOSTRING, "runtime.slicerunetostring",
	       P2(POINTER, SLICE), R1(STRING))

// Convert a string to a []byte.
DEF_GO_RUNTIME(STRINGTOSLICEBYTE, "runtime.stringtoslicebyte",
	       P2(POINTER, STRING), R1(SLICE))

// Convert a string to a []rune.
DEF_GO_RUNTIME(STRINGTOSLICERUNE, "runtime.stringtoslicerune",
	       P2(POINTER, STRING), R1(SLICE))


// Complex division.
DEF_GO_RUNTIME(COMPLEX64_DIV, "__go_complex64_div",
	       P2(COMPLEX64, COMPLEX64), R1(COMPLEX64))
DEF_GO_RUNTIME(COMPLEX128_DIV, "__go_complex128_div",
	       P2(COMPLEX128, COMPLEX128), R1(COMPLEX128))

// Make a slice.
DEF_GO_RUNTIME(MAKESLICE1, "__go_make_slice1", P2(TYPE, UINTPTR), R1(SLICE))
DEF_GO_RUNTIME(MAKESLICE2, "__go_make_slice2", P3(TYPE, UINTPTR, UINTPTR),
	       R1(SLICE))
DEF_GO_RUNTIME(MAKESLICE1BIG, "__go_make_slice1_big", P2(TYPE, UINT64),
	       R1(SLICE))
DEF_GO_RUNTIME(MAKESLICE2BIG, "__go_make_slice2_big", P3(TYPE, UINT64, UINT64),
	       R1(SLICE))


// Make a map.
DEF_GO_RUNTIME(MAKEMAP, "runtime.makemap", P4(TYPE, INT64, POINTER, POINTER),
	       R1(MAP))

// Build a map from a composite literal.
DEF_GO_RUNTIME(CONSTRUCT_MAP, "__go_construct_map",
	       P5(POINTER, UINTPTR, UINTPTR, UINTPTR, POINTER),
	       R1(MAP))

// Look up a key in a map.
DEF_GO_RUNTIME(MAPACCESS1, "runtime.mapaccess1", P3(TYPE, MAP, POINTER),
	       R1(POINTER))

// Look up a key in a map when the value is large.
DEF_GO_RUNTIME(MAPACCESS1_FAT, "runtime.mapaccess1_fat",
	       P4(TYPE, MAP, POINTER, POINTER), R1(POINTER))

// Look up a key in a map returning the value and whether it is
// present.
DEF_GO_RUNTIME(MAPACCESS2, "runtime.mapaccess2", P3(TYPE, MAP, POINTER),
	       R2(POINTER, BOOL))

// Look up a key in a map, returning the value and whether it is
// present, when the value is large.
DEF_GO_RUNTIME(MAPACCESS2_FAT, "runtime.mapaccess2_fat",
	       P4(TYPE, MAP, POINTER, POINTER), R2(POINTER, BOOL))

// Assignment to a key in a map.
DEF_GO_RUNTIME(MAPASSIGN, "runtime.mapassign1",
	       P4(TYPE, MAP, POINTER, POINTER), R0())

// Delete a key from a map.
DEF_GO_RUNTIME(MAPDELETE, "runtime.mapdelete", P3(TYPE, MAP, POINTER), R0())

// Begin a range over a map.
DEF_GO_RUNTIME(MAPITERINIT, "runtime.mapiterinit", P3(TYPE, MAP, POINTER),
	       R0())

// Range over a map, moving to the next map entry.
DEF_GO_RUNTIME(MAPITERNEXT, "runtime.mapiternext", P1(POINTER), R0())


// Make a channel.
DEF_GO_RUNTIME(MAKECHAN, "runtime.makechan", P2(TYPE, INT64), R1(CHAN))

// Send a value on a channel.
DEF_GO_RUNTIME(CHANSEND, "runtime.chansend1", P3(TYPE, CHAN, POINTER), R0())

// Receive a value from a channel.
DEF_GO_RUNTIME(CHANRECV1, "runtime.chanrecv1", P3(TYPE, CHAN, POINTER), R0())

// Receive a value from a channel returning whether it is closed.
DEF_GO_RUNTIME(CHANRECV2, "runtime.chanrecv2", P3(TYPE, CHAN, POINTER),
	       R1(BOOL))


// Start building a select statement.
DEF_GO_RUNTIME(NEWSELECT, "runtime.newselect", P3(POINTER, INT64, INT32), R0())

// Add a default clause to a select statement.
DEF_GO_RUNTIME(SELECTDEFAULT, "runtime.selectdefault",
	       P2(POINTER, INT32), R0())

// Add a send clause to a select statement.
DEF_GO_RUNTIME(SELECTSEND, "runtime.selectsend",
	       P4(POINTER, CHAN, POINTER, INT32), R0())

// Add a receive clause to a select statement, for a clause which does
// not check whether the channel is closed.
DEF_GO_RUNTIME(SELECTRECV, "runtime.selectrecv",
	       P4(POINTER, CHAN, POINTER, INT32), R0())

// Add a receive clause to a select statement, for a clause which does
// check whether the channel is closed.
DEF_GO_RUNTIME(SELECTRECV2, "runtime.selectrecv2",
	       P5(POINTER, CHAN, POINTER, BOOLPTR, INT32), R0())

// Run a select, returning the index of the selected clause.
DEF_GO_RUNTIME(SELECTGO, "runtime.selectgo", P1(POINTER), R1(INT32))


// Panic.
DEF_GO_RUNTIME(PANIC, "__go_panic", P1(EFACE), R0())

// Recover.
DEF_GO_RUNTIME(RECOVER, "__go_recover", P0(), R1(EFACE))

// Recover when called directly from defer.
DEF_GO_RUNTIME(DEFERRED_RECOVER, "__go_deferred_recover", P0(), R1(EFACE))

// Decide whether this function can call recover.
DEF_GO_RUNTIME(CAN_RECOVER, "__go_can_recover", P1(POINTER), R1(BOOL))

// Get the return address of the function.
DEF_GO_RUNTIME(RETURN_ADDRESS, "__go_return_address", P1(INT), R1(POINTER))

// Set the return address for defer in a defer thunk.
DEF_GO_RUNTIME(SET_DEFER_RETADDR, "__go_set_defer_retaddr", P1(POINTER),
	       R1(BOOL))

// Check for a deferred function in an exception handler.
DEF_GO_RUNTIME(CHECK_DEFER, "__go_check_defer", P1(BOOLPTR), R0())

// Run deferred functions.
DEF_GO_RUNTIME(UNDEFER, "__go_undefer", P1(BOOLPTR), R0())

// Panic with a runtime error.
DEF_GO_RUNTIME(RUNTIME_ERROR, "__go_runtime_error", P1(INT32), R0())


// Close.
DEF_GO_RUNTIME(CLOSE, "runtime.closechan", P1(CHAN), R0())


// Copy.
DEF_GO_RUNTIME(COPY, "__go_copy", P3(POINTER, POINTER, UINTPTR), R0())

// Append.
DEF_GO_RUNTIME(APPEND, "__go_append", P4(SLICE, POINTER, UINTPTR, UINTPTR),
	       R1(SLICE))


// Register roots (global variables) for the garbage collector.
DEF_GO_RUNTIME(REGISTER_GC_ROOTS, "__go_register_gc_roots", P1(POINTER), R0())


// Allocate memory.
DEF_GO_RUNTIME(NEW, "__go_new", P2(TYPE, UINTPTR), R1(POINTER))

// Start a new goroutine.
DEF_GO_RUNTIME(GO, "__go_go", P2(FUNC_PTR, POINTER), R0())

// Defer a function.
DEF_GO_RUNTIME(DEFER, "__go_defer", P3(BOOLPTR, FUNC_PTR, POINTER), R0())


// Convert an empty interface to an empty interface, returning ok.
DEF_GO_RUNTIME(IFACEE2E2, "runtime.ifaceE2E2", P1(EFACE), R2(EFACE, BOOL))

// Convert a non-empty interface to an empty interface, returning ok.
DEF_GO_RUNTIME(IFACEI2E2, "runtime.ifaceI2E2", P1(IFACE), R2(EFACE, BOOL))

// Convert an empty interface to a non-empty interface, returning ok.
DEF_GO_RUNTIME(IFACEE2I2, "runtime.ifaceE2I2", P2(TYPE, EFACE),
	       R2(IFACE, BOOL))

// Convert a non-empty interface to a non-empty interface, returning ok.
DEF_GO_RUNTIME(IFACEI2I2, "runtime.ifaceI2I2", P2(TYPE, IFACE),
	       R2(IFACE, BOOL))

// Convert an empty interface to a pointer type, returning ok.
DEF_GO_RUNTIME(IFACEE2T2P, "runtime.ifaceE2T2P", P2(TYPE, EFACE),
	       R2(POINTER, BOOL))

// Convert a non-empty interface to a pointer type, return ok.
DEF_GO_RUNTIME(IFACEI2T2P, "runtime.ifaceI2T2P", P2(TYPE, IFACE),
	       R2(POINTER, BOOL))

// Convert an empty interface to a non-pointer type, returning ok.
DEF_GO_RUNTIME(IFACEE2T2, "runtime.ifaceE2T2", P3(TYPE, EFACE, POINTER),
	       R1(BOOL))

// Convert a non-empty interface to a non-pointer type, returning ok.
DEF_GO_RUNTIME(IFACEI2T2, "runtime.ifaceI2T2", P3(TYPE, IFACE, POINTER),
	       R1(BOOL))

// A type assertion from one interface type to another.  This is
// used for a type assertion.
DEF_GO_RUNTIME(ASSERT_INTERFACE, "__go_assert_interface", P2(TYPE, TYPE), R1(POINTER))

// Convert one interface type to another.  This is used for an
// assignment.
DEF_GO_RUNTIME(CONVERT_INTERFACE, "__go_convert_interface", P2(TYPE, TYPE),
	       R1(POINTER))

// Check whether an interface type may be converted to a
// non-interface type.
DEF_GO_RUNTIME(CHECK_INTERFACE_TYPE, "__go_check_interface_type",
	       P3(TYPE, TYPE, TYPE), R0())

// Return whether we can convert an interface type to a type.
DEF_GO_RUNTIME(IFACEI2TP, "runtime.ifaceI2Tp", P2(TYPE, TYPE), R1(BOOL))

// Get the type descriptor of an empty interface.
DEF_GO_RUNTIME(EFACETYPE, "runtime.efacetype", P1(EFACE), R1(TYPE))

// Get the type descriptor of a non-empty interface.
DEF_GO_RUNTIME(IFACETYPE, "runtime.ifacetype", P1(IFACE), R1(TYPE))


// Compare two type descriptors for equality.
DEF_GO_RUNTIME(IFACETYPEEQ, "runtime.ifacetypeeq", P2(TYPE, TYPE), R1(BOOL))

// Compare two empty interface values.
DEF_GO_RUNTIME(EMPTY_INTERFACE_COMPARE, "__go_empty_interface_compare",
	       P2(EFACE, EFACE), R1(INT))

// Compare an empty interface value to a non-interface value.
DEF_GO_RUNTIME(EMPTY_INTERFACE_VALUE_COMPARE,
	       "__go_empty_interface_value_compare",
	       P3(EFACE, TYPE, POINTER), R1(INT))

// Compare two non-empty interface values.
DEF_GO_RUNTIME(INTERFACE_COMPARE, "__go_interface_compare",
	       P2(IFACE, IFACE), R1(INT))

// Compare a non-empty interface value to a non-interface value.
DEF_GO_RUNTIME(INTERFACE_VALUE_COMPARE, "__go_interface_value_compare",
	       P3(IFACE, TYPE, POINTER), R1(INT))

// Compare a non-empty interface value to an interface value.
DEF_GO_RUNTIME(INTERFACE_EMPTY_COMPARE, "__go_interface_empty_compare",
	       P2(IFACE, EFACE), R1(INT))


// Lock the printer (for print/println).
DEF_GO_RUNTIME(PRINTLOCK, "runtime.printlock", P0(), R0())

// Unlock the printer (for print/println).
DEF_GO_RUNTIME(PRINTUNLOCK, "runtime.printunlock", P0(), R0())

// Print a string (for print/println).
DEF_GO_RUNTIME(PRINTSTRING, "runtime.printstring", P1(STRING), R0())

// Print a uint64 (for print/println).
DEF_GO_RUNTIME(PRINTUINT, "runtime.printuint", P1(UINT64), R0())

// Print a int64 (for print/println).
DEF_GO_RUNTIME(PRINTINT, "runtime.printint", P1(INT64), R0())

// Print a float64 (for print/println).
DEF_GO_RUNTIME(PRINTFLOAT, "runtime.printfloat", P1(FLOAT64), R0())

// Print a complex128 (for print/println).
DEF_GO_RUNTIME(PRINTCOMPLEX, "runtime.printcomplex", P1(COMPLEX128), R0())

// Print a bool (for print/println).
DEF_GO_RUNTIME(PRINTBOOL, "runtime.printbool", P1(BOOL), R0())

// Print a pointer/map/channel/function (for print/println).
DEF_GO_RUNTIME(PRINTPOINTER, "runtime.printpointer", P1(POINTER), R0())

// Print an empty interface (for print/println).
DEF_GO_RUNTIME(PRINTEFACE, "runtime.printeface", P1(EFACE), R0())

// Print a non-empty interface (for print/println).
DEF_GO_RUNTIME(PRINTIFACE, "runtime.printiface", P1(IFACE), R0())

// Print a slice (for print/println).
DEF_GO_RUNTIME(PRINTSLICE, "runtime.printslice", P1(SLICE), R0())

// Print a space (for println).
DEF_GO_RUNTIME(PRINTSP, "runtime.printsp", P0(), R0())

// Print a newline (for println).
DEF_GO_RUNTIME(PRINTNL, "runtime.printnl", P0(), R0())


// Used for field tracking for data analysis.
DEF_GO_RUNTIME(FIELDTRACK, "__go_fieldtrack", P1(POINTER), R0())


// Remove helper macros.
#undef ABFT6
#undef ABFT2
#undef P0
#undef P1
#undef P2
#undef P3
#undef P4
#undef P5
#undef P6
#undef R0
#undef R1
#undef R2
