1 // Basic type definitions for X86 cpus.
3 // Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
9 typedef unsigned char u8;
10 typedef signed char s8;
11 typedef unsigned short u16;
12 typedef signed short s16;
13 typedef unsigned int u32;
14 typedef signed int s32;
15 typedef unsigned long long u64;
16 typedef signed long long s64;
20 struct { u32 hi, lo; };
24 #ifdef MANUAL_NO_JUMP_TABLE
25 # define default case 775324556: asm(""); default
29 # define __VISIBLE __attribute__((externally_visible))
34 #define UNIQSEC __FILE__ "." __stringify(__LINE__)
36 #define __noreturn __attribute__((noreturn))
37 extern void __force_link_error__only_in_32bit_flat(void) __noreturn;
38 extern void __force_link_error__only_in_32bit_segmented(void) __noreturn;
39 extern void __force_link_error__only_in_16bit(void) __noreturn;
41 #define __ASM(code) asm(".section .text.asm." UNIQSEC "\n\t" code)
44 // Notes a function as externally visible in the 16bit code chunk.
45 # define VISIBLE16 __VISIBLE
46 // Notes a function as externally visible in the 32bit flat code chunk.
47 # define VISIBLE32FLAT __section(".discard.func32flat." UNIQSEC) noinline __weak
48 // Notes a function as externally visible in the 32bit segmented code chunk.
49 # define VISIBLE32SEG __section(".discard.func32seg." UNIQSEC) noinline __weak
50 // Designate a variable as (only) visible to 16bit code.
51 # define VAR16 __section(".data16." UNIQSEC)
52 // Designate a variable as visible to 16bit, 32bit, and assembler code.
53 # define VAR16VISIBLE VAR16 __VISIBLE
54 // Designate a variable as externally visible (in addition to all internal code).
55 # define VAR16EXPORT __section(".data16.export." UNIQSEC) __VISIBLE
56 // Designate a variable at a specific 16bit address
57 # define VAR16FIXED(addr) __aligned(1) __VISIBLE __section(".fixedaddr." __stringify(addr))
58 // Designate a variable as (only) visible to 32bit segmented code.
59 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
60 // Designate a 32bit variable also available in 16bit "big real" mode.
61 # define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
62 // Designate top-level assembler as 16bit only.
63 # define ASM16(code) __ASM(code)
64 // Designate top-level assembler as 32bit flat only.
65 # define ASM32FLAT(code)
66 // Compile time check for a given mode.
67 # define ASSERT16() do { } while (0)
68 # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
69 # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
70 #elif MODESEGMENT == 1
71 # define VISIBLE16 __section(".discard.func16." UNIQSEC) noinline __weak
72 # define VISIBLE32FLAT __section(".discard.func32flat." UNIQSEC) noinline __weak
73 # define VISIBLE32SEG __VISIBLE
74 # define VAR16 __section(".discard.var16." UNIQSEC)
75 # define VAR16VISIBLE VAR16 __VISIBLE __weak
76 # define VAR16EXPORT VAR16VISIBLE
77 # define VAR16FIXED(addr) VAR16VISIBLE
78 # define VAR32SEG __section(".data32seg." UNIQSEC)
79 # define VAR32FLATVISIBLE __section(".discard.var32flat." UNIQSEC) __VISIBLE __weak
81 # define ASM32FLAT(code)
82 # define ASSERT16() __force_link_error__only_in_16bit()
83 # define ASSERT32SEG() do { } while (0)
84 # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
86 # define VISIBLE16 __section(".discard.func16." UNIQSEC) noinline __weak
87 # define VISIBLE32FLAT __VISIBLE
88 # define VISIBLE32SEG __section(".discard.func32seg." UNIQSEC) noinline __weak
89 # define VAR16 __section(".discard.var16." UNIQSEC)
90 # define VAR16VISIBLE VAR16 __VISIBLE __weak
91 # define VAR16EXPORT VAR16VISIBLE
92 # define VAR16FIXED(addr) VAR16VISIBLE
93 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
94 # define VAR32FLATVISIBLE __VISIBLE
96 # define ASM32FLAT(code) __ASM(code)
97 # define ASSERT16() __force_link_error__only_in_16bit()
98 # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
99 # define ASSERT32FLAT() do { } while (0)
102 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
103 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
104 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
105 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
106 #define DIV_ROUND_CLOSEST(x, divisor)({ \
107 typeof(divisor) __divisor = divisor; \
108 (((x) + ((__divisor) / 2)) / (__divisor)); \
110 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
111 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
112 #define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1))
113 #define container_of(ptr, type, member) ({ \
114 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
115 (type *)( (char *)__mptr - offsetof(type,member) );})
117 #define likely(x) __builtin_expect(!!(x), 1)
118 #define unlikely(x) __builtin_expect(!!(x), 0)
120 #define NULL ((void*)0)
122 #define __weak __attribute__((weak))
123 #define __section(S) __attribute__((section(S)))
125 #define PACKED __attribute__((packed))
126 #define __aligned(x) __attribute__((aligned(x)))
128 #define barrier() __asm__ __volatile__("": : :"memory")
130 #define noinline __attribute__((noinline))
131 #define __always_inline inline __attribute__((always_inline))
132 #define __malloc __attribute__((__malloc__))
133 #define __attribute_const __attribute__((__const__))
135 #define __stringify_1(x) #x
136 #define __stringify(x) __stringify_1(x)