Built-in assembler

The built-in Intel x86-64 assembler facilitates performant implementations of numerical methods. The following example shows a function for negating integers implemented using the built-in assembler. The function then is used to negate the number 42.

Function definition

Here is an example function negating an integer:

(use-modules (oop goops) (aiscm asm) (aiscm int))
(define ctx (make <context>))
(define f (asm ctx <int> (list <int>) (list (MOV EAX EDI) (NEG EAX) (RET))))
(f 42)
; -42

This will generate and call an identity function with the following assembler code

mov eax,edi
neg eax
ret

It is also possible to use jump statements and labels:

(use-modules (oop goops) (aiscm asm) (aiscm int))
(define ctx (make <context>))
(define f (asm ctx <int> (list <int>) (list (MOV EAX EDI) (CMP EAX 0) (JNLE 'skip) (NEG EAX) 'skip (RET))))
(f -42)
; 42
(f 42)
; 42

This will generate and call a function for computing the absolute value. The assembler code is as follows

mov eax, edi
cmp eax, 0x0
jg 0xb
neg eax
ret

Setting the environment variable DEBUG to YES will cause the JIT compiler to display the assembler code using the objdump command line tool.

Virtual registers

The jit function provides a higher level interface with virtual registers and operator mappings. The following example defines a function for adding two integers:

(use-modules (oop goops) (aiscm asm) (aiscm jit) (aiscm int))
(define ctx (make <context>))
(define f (jit ctx (list <int> <int>) (lambda (x y) (+ x y))))
(f 12 34)
; 46 

The jit function also instantiates loops for array processing. For example:

(use-modules (oop goops) (aiscm asm) (aiscm jit) (aiscm int) (aiscm rgb) (aiscm sequence))
(define ctx (make <context>))
(define f (jit ctx (list (rgb <int>) (sequence <int>)) +))
(f (rgb 1 2 3) (seq <int> 4 5 6))
; #<sequence<rgb<int<32,signed>>>>:
; ((rgb 5 6 7) (rgb 6 7 8) (rgb 7 8 9))

Interpreter callbacks

Callbacks into the GNU Guile interpreter are defined as well. They can be used to generate machine code which handles Scheme objects:

(use-modules (oop goops) (aiscm asm) (aiscm jit) (aiscm int) (aiscm obj))
(define ctx (make <context>))
(define f (jit ctx (list <int> <obj>) +))
(f 1 (/ 2 3))
; 5/3

Generated by Pandoc - 2017-12-03