! Copyright (C) 2005 Slava Pestov. ! See http://factor.sf.net/license.txt for BSD license. IN: compiler-backend USING: alien assembler compiler inference kernel kernel-internals lists math memory namespaces sequences words ; GENERIC: v>operand M: integer v>operand address ; M: vreg v>operand vreg-n { EAX ECX EDX } nth ; : dest/src ( vop -- dest src ) dup vop-dest v>operand swap vop-source v>operand ; ! Not used on x86 M: %prologue generate-node drop ; : compile-c-call ( symbol dll -- ) 2dup dlsym CALL t rel-dlsym ; M: %call generate-node ( vop -- ) vop-label dup postpone-word CALL ; M: %jump-label generate-node ( vop -- ) vop-label JMP ; M: %call-label generate-node ( vop -- ) vop-label CALL ; M: %jump generate-node ( vop -- ) vop-label dup postpone-word JMP ; M: %jump-f generate-node ( vop -- ) dup vop-source v>operand f address CMP vop-label JE ; M: %jump-t generate-node ( vop -- ) dup vop-source v>operand f address CMP vop-label JNE ; M: %return-to generate-node ( vop -- ) 0 PUSH vop-label absolute ; M: %return generate-node ( vop -- ) drop RET ; M: %untag generate-node ( vop -- ) vop-dest v>operand BIN: 111 bitnot AND ; M: %tag-fixnum generate-node ( vop -- ) vop-dest v>operand 3 SHL ; M: %untag-fixnum generate-node ( vop -- ) vop-dest v>operand 3 SHR ; M: %slot generate-node ( vop -- ) #! the untagged object is in vop-dest, the tagged slot #! number is in vop-source. dest/src ! turn tagged fixnum slot # into an offset, multiple of 4 dup 1 SHR ! compute slot address in vop-dest dupd ADD ! load slot value in vop-dest dup unit MOV ; M: %fast-slot generate-node ( vop -- ) #! the tagged object is in vop-dest, the pointer offset is #! in vop-literal. the offset already takes the type tag #! into account, so its just one instruction to load. dup vop-literal swap vop-dest v>operand tuck >r 2list r> swap MOV ; M: %set-slot generate-node ( vop -- ) #! the untagged object is in vop-dest, the new value is in #! vop-source, the tagged slot number is in vop-literal. dup vop-literal v>operand over vop-dest v>operand ! turn tagged fixnum slot # into an offset, multiple of 4 over 1 SHR ! compute slot address in vop-dest dupd ADD ! store new slot value >r vop-source v>operand r> unit swap MOV ; M: %fast-set-slot generate-node ( vop -- ) #! the tagged object is in vop-dest, the new value is in #! vop-source, the pointer offset is in vop-literal. the #! offset already takes the type tag into account, so its #! just one instruction to load. dup vop-literal over vop-dest v>operand swap 2list swap vop-source v>operand MOV ; M: %dispatch generate-node ( vop -- ) #! Compile a piece of code that jumps to an offset in a #! jump table indexed by the fixnum at the top of the stack. #! The jump table must immediately follow this macro. vop-source v>operand ! Multiply by 4 to get a jump table offset dup 2 SHL ! Add to jump table base dup HEX: ffff ADD just-compiled >r f rel-address ! Jump to jump table entry unit JMP ! Align for better performance compile-aligned ! Fix up jump table pointer compiled-offset r> set-compiled-cell ( fixup -- ) ; M: %type generate-node ( vop -- ) #! Intrinstic version of type primitive. It outputs an #! UNBOXED value in vop-dest.