1 // Basic type definitions for X86 cpus.
3 // Copyright (C) 2008-2010 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 lo, hi; };
24 // Definition for common 16bit segment/offset pointers.
35 #ifdef MANUAL_NO_JUMP_TABLE
36 # define default case 775324556: asm(""); default
40 # define __VISIBLE __attribute__((externally_visible))
45 #define UNIQSEC __FILE__ "." __stringify(__LINE__)
47 #define __noreturn __attribute__((noreturn))
48 extern void __force_link_error__only_in_32bit_flat(void) __noreturn;
49 extern void __force_link_error__only_in_32bit_segmented(void) __noreturn;
50 extern void __force_link_error__only_in_16bit(void) __noreturn;
52 #define __ASM(code) asm(".section .text.asm." UNIQSEC "\n\t" code)
55 // Notes a function as externally visible in the 16bit code chunk.
56 # define VISIBLE16 __VISIBLE
57 // Notes a function as externally visible in the 32bit flat code chunk.
58 # define VISIBLE32FLAT
59 // Notes a 32bit flat function that will only be called during init.
60 # define VISIBLE32INIT
61 // Notes a function as externally visible in the 32bit segmented code chunk.
63 // Designate a variable as (only) visible to 16bit code.
64 # define VAR16 __section(".data16." UNIQSEC)
65 // Designate a variable as (only) visible to 32bit segmented code.
66 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
67 // Designate a variable as visible and located in the e-segment.
68 # define VARLOW __section(".discard.varlow." UNIQSEC) __VISIBLE __weak
69 // Designate a variable as visible and located in the f-segment.
70 # define VARFSEG __section(".discard.varfseg." UNIQSEC) __VISIBLE __weak
71 // Designate a variable at a specific address in the f-segment.
72 # define VARFSEGFIXED(addr) __section(".discard.varfixed." UNIQSEC) __VISIBLE __weak
73 // Verify a variable is only accessable via 32bit "init" functions
74 # define VARVERIFY32INIT __section(".discard.varinit." UNIQSEC)
75 // Designate top-level assembler as 16bit only.
76 # define ASM16(code) __ASM(code)
77 // Designate top-level assembler as 32bit flat only.
78 # define ASM32FLAT(code)
79 // Compile time check for a given mode.
80 # define ASSERT16() do { } while (0)
81 # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
82 # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
83 #elif MODESEGMENT == 1
85 # define VISIBLE32FLAT
86 # define VISIBLE32INIT
87 # define VISIBLE32SEG __VISIBLE
88 # define VAR16 __section(".discard.var16." UNIQSEC)
89 # define VAR32SEG __section(".data32seg." UNIQSEC)
90 # define VARLOW __section(".discard.varlow." UNIQSEC) __VISIBLE __weak
91 # define VARFSEG __section(".discard.varfseg." UNIQSEC) __VISIBLE __weak
92 # define VARFSEGFIXED(addr) __section(".discard.varfixed." UNIQSEC) __VISIBLE __weak
93 # define VARVERIFY32INIT __section(".discard.varinit." UNIQSEC)
95 # define ASM32FLAT(code)
96 # define ASSERT16() __force_link_error__only_in_16bit()
97 # define ASSERT32SEG() do { } while (0)
98 # define ASSERT32FLAT() __force_link_error__only_in_32bit_flat()
101 # define VISIBLE32FLAT __section(".text.runtime." UNIQSEC) __VISIBLE
102 # define VISIBLE32INIT __section(".text.init." UNIQSEC) __VISIBLE
103 # define VISIBLE32SEG
104 # define VAR16 __section(".discard.var16." UNIQSEC)
105 # define VAR32SEG __section(".discard.var32seg." UNIQSEC)
106 # define VARLOW __section(".data.varlow." UNIQSEC) __VISIBLE __weak
107 # define VARFSEG __section(".data.varfseg." UNIQSEC) __VISIBLE
108 # define VARFSEGFIXED(addr) __section(".fixedaddr." __stringify(addr)) __VISIBLE __aligned(1)
109 # define VARVERIFY32INIT __section(".data.varinit." UNIQSEC)
111 # define ASM32FLAT(code) __ASM(code)
112 # define ASSERT16() __force_link_error__only_in_16bit()
113 # define ASSERT32SEG() __force_link_error__only_in_32bit_segmented()
114 # define ASSERT32FLAT() do { } while (0)
117 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
118 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
119 #define FIELD_SIZEOF(t, f) (sizeof(((t*)0)->f))
120 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
121 #define DIV_ROUND_CLOSEST(x, divisor)({ \
122 typeof(divisor) __divisor = divisor; \
123 (((x) + ((__divisor) / 2)) / (__divisor)); \
125 #define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1)
126 #define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
127 #define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1))
128 #define container_of(ptr, type, member) ({ \
129 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
130 (type *)( (char *)__mptr - offsetof(type,member) );})
131 #define container_of_or_null(ptr, type, member) ({ \
132 const typeof( ((type *)0)->member ) *___mptr = (ptr); \
133 ___mptr ? container_of(___mptr, type, member) : NULL; })
135 #define likely(x) __builtin_expect(!!(x), 1)
136 #define unlikely(x) __builtin_expect(!!(x), 0)
138 #define NULL ((void*)0)
140 #define __weak __attribute__((weak))
141 #define __section(S) __attribute__((section(S)))
143 #define PACKED __attribute__((packed))
144 #define __aligned(x) __attribute__((aligned(x)))
146 #define barrier() __asm__ __volatile__("": : :"memory")
148 #define noinline __attribute__((noinline))
149 #define __always_inline inline __attribute__((always_inline))
150 #define __malloc __attribute__((__malloc__))
151 #define __attribute_const __attribute__((__const__))
153 #define __stringify_1(x) #x
154 #define __stringify(x) __stringify_1(x)