initial commit: a mess of assembly code
[fmap.git] / x86_64_sse2_x87 / fasm / source / windows / dll / selfhost.inc
blobd21f18822a8c4377e6999d24b9300c6eb5d1a644
1 \r
2 include '../../../examples/x86/include/80386.inc'\r
3 \r
4 macro format?.PE? settings\r
5         PE.Settings.Characteristics = IMAGE_FILE_EXECUTABLE_IMAGE or IMAGE_FILE_32BIT_MACHINE or IMAGE_FILE_LINE_NUMS_STRIPPED or IMAGE_FILE_LOCAL_SYMS_STRIPPED\r
6         PE.Settings.DllCharacteristics = 0\r
7         PE.Settings.Stamp = +VERSION\r
8         PE.Settings.LegacyHeaders = 0\r
9         local seq\r
10         define seq settings:\r
11         while 1\r
12                 match :, seq\r
13                         break\r
14                 else match =DLL? more, seq\r
15                         PE.Settings.Characteristics = PE.Settings.Characteristics or IMAGE_FILE_DLL\r
16                         redefine seq more\r
17                 else match =large? more, seq\r
18                         PE.Settings.Characteristics = PE.Settings.Characteristics or IMAGE_FILE_LARGE_ADDRESS_AWARE\r
19                         redefine seq more\r
20                 else match =WDM? more, seq\r
21                         PE.Settings.DllCharacteristics = PE.Settings.DllCharacteristics or IMAGE_DLLCHARACTERISTICS_WDM_DRIVER\r
22                         redefine seq more\r
23                 else match =NX? more, seq\r
24                         PE.Settings.DllCharacteristics = PE.Settings.DllCharacteristics or IMAGE_DLLCHARACTERISTICS_NX_COMPAT\r
25                         redefine seq more\r
26                 else match =at? base =on? stub :, seq\r
27                         PE.Settings.ImageBase = base\r
28                         PE.Settings.Stub = stub\r
29                         break\r
30                 else match =at? base :, seq\r
31                         PE.Settings.ImageBase = base\r
32                         break\r
33                 else match =on? stub :, seq\r
34                         PE.Settings.Stub = stub\r
35                         break\r
36                 else\r
37                         match =GUI? more, seq\r
38                                 PE.Settings.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_GUI\r
39                                 redefine seq more\r
40                         else match =console? more, seq\r
41                                 PE.Settings.Subsystem = IMAGE_SUBSYSTEM_WINDOWS_CUI\r
42                                 redefine seq more\r
43                         else match =native? more, seq\r
44                                 PE.Settings.Subsystem = IMAGE_SUBSYSTEM_NATIVE\r
45                                 PE.Settings.SectionAlignment = 32\r
46                                 PE.Settings.FileAlignment = 32\r
47                                 redefine seq more\r
48                         else match =EFI? more, seq\r
49                                 PE.Settings.Magic = 0x20B\r
50                                 PE.Settings.Subsystem = IMAGE_SUBSYSTEM_EFI_APPLICATION\r
51                                 redefine seq more\r
52                         else match =EFIboot? more, seq\r
53                                 PE.Settings.Magic = 0x20B\r
54                                 PE.Settings.Subsystem = IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER\r
55                                 redefine seq more\r
56                         else match =EFIruntime? more, seq\r
57                                 PE.Settings.Magic = 0x20B\r
58                                 PE.Settings.Subsystem = IMAGE_SUBSYSTEM_EFI_RUNTIME_DRIVER\r
59                                 redefine seq more\r
60                         else\r
61                                 err 'invalid argument'\r
62                                 break\r
63                         end match\r
64                         match V.v more, seq\r
65                                 PE.Settings.MajorSubsystemVersion = V\r
66                                 PE.Settings.MinorSubsystemVersion = v\r
67                                 redefine seq more\r
68                         end match\r
69                 end match\r
70         end while\r
71         if PE.Settings.Characteristics and IMAGE_FILE_DLL\r
72                 format binary as 'dll'\r
73         else\r
74                 format binary as 'exe'\r
75         end if\r
76         include '../../../examples/x86/include/format/pe.inc'\r
77         use32\r
78 end macro\r
80 macro struct? name\r
81         macro ends?!\r
82                         end namespace\r
83                 esc end struc\r
84                 virtual at 0\r
85                         name name\r
86                         sizeof.name = $\r
87                 end virtual\r
88                 purge ends?\r
89         end macro\r
90         esc struc name\r
91                 label . : sizeof.name\r
92                 namespace .\r
93 end macro\r
95 calminstruction invoke? proc*,args&\r
96         local   tmp, tmpst, stack\r
97         match   , args\r
98         jyes    go\r
99     collect:\r
100         match   tmpst=,args, args\r
101         take    stack, tmpst\r
102         jyes    collect\r
103     push:\r
104         match   tmp], args\r
105         jyes    regular\r
106         check   args relativeto ebp & args - ebp\r
107         jno     regular\r
108         arrange tmp, =push =eax\r
109         assemble tmp\r
110         arrange tmp, =lea =eax,[args]\r
111         assemble tmp\r
112         arrange tmp, =xchg =eax,[=esp]\r
113         assemble tmp\r
114         jump    next\r
115       regular:\r
116         arrange tmp, =pushd args\r
117         assemble tmp\r
118       next:\r
119         take    args, stack\r
120         jyes    push\r
121     go:\r
122         arrange tmp, =call [proc]\r
123         assemble tmp\r
124 end calminstruction\r
126 calminstruction mov? dest*,src*\r
127         local   tmp\r
128         match   tmp], src\r
129         jyes    regular\r
130         check   src relativeto ebp & src - ebp\r
131         jno     regular\r
132         arrange tmp, =lea dest,[src]\r
133         assemble tmp\r
134         exit\r
135     regular:\r
136         arrange tmp, =mov dest,src\r
137         assemble tmp\r
138 end calminstruction\r
140 macro library? definitions&\r
141         PE.Imports:\r
142         iterate <name,string>, definitions\r
143               if ~ name.redundant\r
144                       dd RVA name.lookup,0,0,RVA name.str,RVA name.address\r
145               end if\r
146               name.referred = 1\r
147         end iterate\r
148         dd 0,0,0,0,0\r
149         iterate <name,string>, definitions\r
150               if ~ name.redundant\r
151                       name.str db string,0\r
152                                align 2\r
153               end if\r
154         end iterate\r
155 end macro\r
157 macro import? name,definitions&\r
158         align 4\r
159         if defined name.referred\r
160                 name.lookup:\r
161                 iterate <label,string>, definitions\r
162                         if used label\r
163                                 if string eqtype ''\r
164                                         dd RVA name.label\r
165                                 else\r
166                                         dd 80000000h + string\r
167                                 end if\r
168                         end if\r
169                 end iterate\r
170                 if $ > name.lookup\r
171                         name.redundant = 0\r
172                         dd 0\r
173                 else\r
174                         name.redundant = 1\r
175                 end if\r
176                 name.address:\r
177                 iterate <label,string>, definitions\r
178                         if used label\r
179                                 if string eqtype ''\r
180                                         label dd RVA name.label\r
181                                 else\r
182                                         label dd 80000000h + string\r
183                                 end if\r
184                         end if\r
185                 end iterate\r
186                 if ~ name.redundant\r
187                         dd 0\r
188                 end if\r
189                 iterate <label,string>, definitions\r
190                         if used label & string eqtype ''\r
191                                 name.label dw 0\r
192                                            db string,0\r
193                                            align 2\r
194                         end if\r
195                 end iterate\r
196         end if\r
197 end macro\r
199 macro export dllname,exports&\r
200         iterate <label,string>, exports\r
202                 local module,addresses,names,ordinal,count\r
203                 count = %%\r
204                 dd 0,0,0,RVA module,1\r
205                 dd count,count,RVA addresses,RVA names,RVA ordinal\r
206                 addresses:\r
207                 repeat count\r
208                         indx %\r
209                         dd RVA label\r
210                 end repeat\r
211                 names:\r
212                 repeat count\r
213                         dd RVA names.name#%\r
214                 end repeat\r
215                 ordinal:\r
216                 repeat count\r
217                         dw %-1\r
218                 end repeat\r
219                 module db dllname,0\r
220                 repeat count\r
221                         indx %\r
222                         names.name#% db string,0\r
223                 end repeat\r
225                 local x,y,z,str1,str2,v1,v2\r
226                 x = count shr 1\r
227                 while x > 0\r
228                         y = x\r
229                         while y < count\r
230                                 z = y\r
231                                 while z-x >= 0\r
232                                         load v1:dword from names+z*4\r
233                                         str1 = ($-(RVA $))+v1\r
234                                         load v2:dword from names+(z-x)*4\r
235                                         str2 = ($-(RVA $))+v2\r
236                                         while v1 > 0\r
237                                                 load v1:byte from str1+%-1\r
238                                                 load v2:byte from str2+%-1\r
239                                                 if v1 <> v2\r
240                                                         break\r
241                                                 end if\r
242                                         end while\r
243                                         if v1 < v2\r
244                                                 load v1:dword from names+z*4\r
245                                                 load v2:dword from names+(z-x)*4\r
246                                                 store v1:dword at names+(z-x)*4\r
247                                                 store v2:dword at names+z*4\r
248                                                 load v1:word from ordinal+z*2\r
249                                                 load v2:word from ordinal+(z-x)*2\r
250                                                 store v1:word at ordinal+(z-x)*2\r
251                                                 store v2:word at ordinal+z*2\r
252                                         else\r
253                                                 break\r
254                                         end if\r
255                                         z = z-x\r
256                                 end while\r
257                                 y = y+1\r
258                         end while\r
259                         x = x shr 1\r
260                 end while\r
262                 break\r
263         end iterate\r
264 end macro\r
266 include '../kernel32.inc'\r