Botan  1.11.15
src/lib/utils/asm_x86_32/asm_x86_32.h
Go to the documentation of this file.
00001 /*
00002 * Assembly Macros for 32-bit x86
00003 * (C) 1999-2008 Jack Lloyd
00004 *
00005 * Botan is released under the Simplified BSD License (see license.txt)
00006 */
00007 
00008 #ifndef BOTAN_ASM_MACROS_X86_32_H__
00009 #define BOTAN_ASM_MACROS_X86_32_H__
00010 
00011 /*
00012 * General/Global Macros
00013 */
00014 #define ALIGN .p2align 4,,15
00015 
00016 #define START_LISTING(FILENAME) \
00017    .file #FILENAME;             \
00018    .text;                       \
00019    ALIGN;
00020 
00021 #if defined(__ELF__)
00022 .section .note.GNU-stack,"",%progbits
00023 #endif
00024 
00025 /*
00026 * Function Definitions
00027 */
00028 #define START_FUNCTION(func_name) \
00029    ALIGN;                         \
00030    .global  func_name;            \
00031    .type    func_name,@function;  \
00032 func_name:
00033 
00034 #define END_FUNCTION(func_name) \
00035    ret
00036 
00037 /*
00038 * Loop Control
00039 */
00040 #define START_LOOP(LABEL) \
00041    ALIGN;                 \
00042    LABEL##_LOOP:
00043 
00044 #define LOOP_UNTIL_EQ(REG, NUM, LABEL) \
00045    cmpl IMM(NUM), REG;                 \
00046    jne LABEL##_LOOP
00047 
00048 #define LOOP_UNTIL_LT(REG, NUM, LABEL) \
00049    cmpl IMM(NUM), REG;                 \
00050    jge LABEL##_LOOP
00051 
00052 /*
00053  Conditional Jumps
00054 */
00055 #define JUMP_IF_ZERO(REG, LABEL) \
00056    cmpl IMM(0), REG;             \
00057    jz LABEL
00058 
00059 #define JUMP_IF_LT(REG, NUM, LABEL) \
00060    cmpl IMM(NUM), REG;              \
00061    jl LABEL
00062 
00063 /*
00064 * Register Names
00065 */
00066 #define EAX %eax
00067 #define EBX %ebx
00068 #define ECX %ecx
00069 #define EDX %edx
00070 #define EBP %ebp
00071 #define EDI %edi
00072 #define ESI %esi
00073 #define ESP %esp
00074 
00075 /*
00076 * Memory Access Operations
00077 */
00078 #define ARRAY1(REG, NUM) (NUM)(REG)
00079 #define ARRAY4(REG, NUM) 4*(NUM)(REG)
00080 #define ARRAY4_INDIRECT(BASE, OFFSET, NUM) 4*(NUM)(BASE,OFFSET,4)
00081 #define ARG(NUM) 4*(PUSHED) + ARRAY4(ESP, NUM)
00082 
00083 #define ASSIGN(TO, FROM) movl FROM, TO
00084 #define ASSIGN_BYTE(TO, FROM) movzbl FROM, TO
00085 
00086 #define PUSH(REG) pushl REG
00087 #define POP(REG) popl REG
00088 
00089 #define SPILL_REGS() \
00090    PUSH(EBP) ; \
00091    PUSH(EDI) ; \
00092    PUSH(ESI) ; \
00093    PUSH(EBX)
00094 
00095 #define RESTORE_REGS() \
00096    POP(EBX) ;  \
00097    POP(ESI) ;  \
00098    POP(EDI) ;  \
00099    POP(EBP)
00100 
00101 /*
00102 * ALU Operations
00103 */
00104 #define IMM(VAL) $VAL
00105 
00106 #define ADD(TO, FROM) addl FROM, TO
00107 #define ADD_IMM(TO, NUM) ADD(TO, IMM(NUM))
00108 #define ADD_W_CARRY(TO1, TO2, FROM) addl FROM, TO1; adcl IMM(0), TO2;
00109 #define SUB_IMM(TO, NUM) subl IMM(NUM), TO
00110 #define ADD2_IMM(TO, FROM, NUM) leal NUM(FROM), TO
00111 #define ADD3_IMM(TO, FROM, NUM) leal NUM(TO,FROM,1), TO
00112 #define MUL(REG) mull REG
00113 
00114 #define SHL_IMM(REG, SHIFT) shll IMM(SHIFT), REG
00115 #define SHR_IMM(REG, SHIFT) shrl IMM(SHIFT), REG
00116 #define SHL2_3(TO, FROM) leal 0(,FROM,8), TO
00117 
00118 #define XOR(TO, FROM) xorl FROM, TO
00119 #define AND(TO, FROM) andl FROM, TO
00120 #define OR(TO, FROM) orl FROM, TO
00121 #define NOT(REG) notl REG
00122 #define ZEROIZE(REG) XOR(REG, REG)
00123 
00124 #define ROTL_IMM(REG, NUM) roll IMM(NUM), REG
00125 #define ROTR_IMM(REG, NUM) rorl IMM(NUM), REG
00126 #define BSWAP(REG) bswapl REG
00127 
00128 #endif