11  Appendix: Builtin, Special and MATH1 functions

11.1 Builtin Functions

According to Tierney (2023) :

Calls to functions known at compile time to be of type BUILTIN can be handled 
more efficiently. The interpreter evaluates all arguments for BUILTIN functions 
before calling the function, so the compiler can evaluate the arguments in 
line without the need for creating promises.

Many built-in functions have their own bytecode instruction e.g. ISCHARACTER for is.character() and MATH1 cos for cos().

Other built-in functions should be built via the instructions GETBUILTIN (Section 9.42) and CALLBUILTIN (Section 9.9)

11.1.1 List of all base package built-in functions

basevars <- ls('package:base', all.names = TRUE)
types <- sapply(basevars, function(n) typeof(get(n)))
names(types)[types == 'builtin'] |> dput()
c("-", ":", "!", "!=", "...elt", "...length", "...names", ".C", 
".cache_class", ".Call", ".Call.graphics", ".class2", ".External", 
".External.graphics", ".External2", ".Fortran", ".isMethodsDispatchOn", 
".Primitive", ".primTrace", ".primUntrace", ".subset", ".subset2", 
"(", "*", "/", "&", "%*%", "%/%", "%%", "^", "+", "<", "<=", 
"==", ">", ">=", "|", "abs", "acos", "acosh", "all", "any", "anyNA", 
"Arg", "as.call", "as.character", "as.complex", "as.double", 
"as.environment", "as.integer", "as.logical", "as.numeric", "as.raw", 
"asin", "asinh", "atan", "atanh", "attr", "attr<-", "attributes", 
"attributes<-", "baseenv", "browser", "c", "ceiling", "class", 
"class<-", "Conj", "cos", "cosh", "cospi", "cummax", "cummin", 
"cumprod", "cumsum", "digamma", "dim", "dim<-", "dimnames", "dimnames<-", 
"emptyenv", "enc2native", "enc2utf8", "environment<-", "exp", 
"expm1", "floor", "gamma", "gc.time", "globalenv", "Im", "interactive", 
"invisible", "is.array", "is.atomic", "is.call", "is.character", 
"is.complex", "is.double", "is.environment", "is.expression", 
"is.finite", "is.function", "is.infinite", "is.integer", "is.language", 
"is.list", "is.logical", "is.matrix", "is.na", "is.name", "is.nan", 
"is.null", "is.numeric", "is.object", "is.pairlist", "is.raw", 
"is.recursive", "is.single", "is.symbol", "isS4", "lazyLoadDBfetch", 
"length", "length<-", "levels<-", "lgamma", "list", "log10", 
"log1p", "log2", "max", "min", "Mod", "names", "names<-", "nargs", 
"nzchar", "oldClass", "oldClass<-", "pos.to.env", "proc.time", 
"prod", "range", "Re", "retracemem", "seq_along", "seq_len", 
"seq.int", "sign", "sin", "sinh", "sinpi", "sqrt", "standardGeneric", 
"storage.mode<-", "sum", "tan", "tanh", "tanpi", "tracemem", 
"trigamma", "trunc", "unCfillPOSIXlt", "unclass", "untracemem", 
"xtfrm")

11.2 Special Functions

Special functions also have a custom instruction for executing a call: CALLSPECIAL (Section 9.10)

11.2.1 List of all base package special functions

basevars <- ls('package:base', all.names = TRUE)
types <- sapply(basevars, function(n) typeof(get(n)))
names(types)[types == 'special'] |> dput()
c("::", ":::", ".Internal", "[", "[[", "[[<-", "[<-", "{", "@", 
"@<-", "&&", "<-", "<<-", "=", "||", "~", "$", "$<-", "break", 
"call", "expression", "for", "forceAndCall", "function", "if", 
"log", "missing", "next", "on.exit", "quote", "rep", "repeat", 
"return", "round", "signif", "substitute", "switch", "UseMethod", 
"while")

11.3 MATH1 functions

The MATH instruction understands the following operations

compiler:::math1funs
 [1] "floor"    "ceiling"  "sign"     "expm1"    "log1p"    "cos"     
 [7] "sin"      "tan"      "acos"     "asin"     "atan"     "cosh"    
[13] "sinh"     "tanh"     "acosh"    "asinh"    "atanh"    "lgamma"  
[19] "gamma"    "digamma"  "trigamma" "cospi"    "sinpi"    "tanpi"   

E.g.

code <- r"(
LDCONST 1.54
MATH1 ceiling
RETURN
)"

asm(code) |> eval()
[1] 2