compiler.pas 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Florian Klaempfl
  4. This unit is the interface of the compiler which can be used by
  5. external programs to link in the compiler
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. ****************************************************************************}
  18. unit compiler;
  19. {$i fpcdefs.inc}
  20. {$ifdef FPC}
  21. { One of Alpha, I386 or M68K must be defined }
  22. {$UNDEF CPUOK}
  23. {$ifdef I386}
  24. {$define CPUOK}
  25. {$endif}
  26. {$ifdef M68K}
  27. {$ifndef CPUOK}
  28. {$DEFINE CPUOK}
  29. {$else}
  30. {$fatal cannot define two CPU switches}
  31. {$endif}
  32. {$endif}
  33. {$ifdef alpha}
  34. {$ifndef CPUOK}
  35. {$DEFINE CPUOK}
  36. {$else}
  37. {$fatal cannot define two CPU switches}
  38. {$endif}
  39. {$endif}
  40. {$ifdef vis}
  41. {$ifndef CPUOK}
  42. {$DEFINE CPUOK}
  43. {$else}
  44. {$fatal cannot define two CPU switches}
  45. {$endif}
  46. {$endif}
  47. {$ifdef powerpc}
  48. {$ifndef CPUOK}
  49. {$DEFINE CPUOK}
  50. {$else}
  51. {$fatal cannot define two CPU switches}
  52. {$endif}
  53. {$endif}
  54. {$ifdef ia64}
  55. {$ifndef CPUOK}
  56. {$DEFINE CPUOK}
  57. {$else}
  58. {$fatal cannot define two CPU switches}
  59. {$endif}
  60. {$endif}
  61. {$ifdef SPARC}
  62. {$ifndef CPUOK}
  63. {$DEFINE CPUOK}
  64. {$else}
  65. {$fatal cannot define two CPU switches}
  66. {$endif}
  67. {$endif}
  68. {$ifdef x86_64}
  69. {$ifndef CPUOK}
  70. {$DEFINE CPUOK}
  71. {$else}
  72. {$fatal cannot define two CPU switches}
  73. {$endif}
  74. {$endif}
  75. {$ifdef ARM}
  76. {$ifndef CPUOK}
  77. {$DEFINE CPUOK}
  78. {$else}
  79. {$fatal cannot define two CPU switches}
  80. {$endif ARM}
  81. {$endif ARM}
  82. {$ifndef CPUOK}
  83. {$fatal One of the switches I386, iA64, Alpha, PowerPC or M68K must be defined}
  84. {$endif}
  85. {$ifdef support_mmx}
  86. {$ifndef i386}
  87. {$fatal I386 switch must be on for MMX support}
  88. {$endif i386}
  89. {$endif support_mmx}
  90. {$endif}
  91. interface
  92. uses
  93. {$ifdef fpc}
  94. {$ifdef GO32V2}
  95. emu387,
  96. {$endif GO32V2}
  97. {$ifdef WATCOM} // wiktor: pewnie nie potrzeba
  98. emu387,
  99. { dpmiexcp, }
  100. {$endif WATCOM}
  101. {$endif}
  102. {$ifdef USEEXCEPT}
  103. tpexcept,
  104. {$endif USEEXCEPT}
  105. {$ifdef BrowserLog}
  106. browlog,
  107. {$endif BrowserLog}
  108. {$IFDEF USE_SYSUTILS}
  109. {$ELSE USE_SYSUTILS}
  110. dos,
  111. {$ENDIF USE_SYSUTILS}
  112. verbose,comphook,systems,
  113. cutils,cclasses,globals,options,fmodule,parser,symtable,
  114. assemble,link,import,export,tokens,pass_1
  115. { cpu overrides }
  116. ,cpuswtch
  117. { cpu codegenerator }
  118. ,cgcpu
  119. {$ifndef NOPASS2}
  120. ,cpunode
  121. {$endif}
  122. { cpu targets }
  123. ,cputarg
  124. { cpu parameter handling }
  125. ,cpupara
  126. { procinfo stuff }
  127. ,cpupi
  128. { system information for source system }
  129. { the information about the target os }
  130. { are pulled in by the t_* units }
  131. {$ifdef amiga}
  132. ,i_amiga
  133. {$endif amiga}
  134. {$ifdef atari}
  135. ,i_atari
  136. {$endif atari}
  137. {$ifdef beos}
  138. ,i_beos
  139. {$endif beos}
  140. {$ifdef fbds}
  141. ,i_fbsd
  142. {$endif fbds}
  143. {$ifdef go32v2}
  144. ,i_go32v2
  145. {$endif go32v2}
  146. {$ifdef linux}
  147. ,i_linux
  148. {$endif linux}
  149. {$ifdef macos}
  150. ,i_macos
  151. {$endif macos}
  152. {$ifdef nwm}
  153. ,i_nwm
  154. {$endif nwm}
  155. {$ifdef nwl}
  156. ,i_nwl
  157. {$endif nwm}
  158. {$ifdef os2}
  159. {$ifdef emx}
  160. ,i_emx
  161. {$else emx}
  162. ,i_os2
  163. {$endif emx}
  164. {$endif os2}
  165. {$ifdef palmos}
  166. ,i_palmos
  167. {$endif palmos}
  168. {$ifdef sunos}
  169. ,i_sunos
  170. {$endif sunos}
  171. {$ifdef wdosx}
  172. ,i_wdosx
  173. {$endif wdosx}
  174. {$ifdef win32}
  175. ,i_win32
  176. {$endif win32}
  177. { assembler readers }
  178. {$ifdef i386}
  179. {$ifndef NoRa386Int}
  180. ,ra386int
  181. {$endif NoRa386Int}
  182. {$ifndef NoRa386Att}
  183. ,ra386att
  184. {$endif NoRa386Att}
  185. {$else}
  186. ,rasm
  187. {$endif i386}
  188. {$ifdef powerpc}
  189. ,rappcgas
  190. {$endif powerpc}
  191. {$ifdef x86_64}
  192. ,rax64att
  193. {$endif x86_64}
  194. {$ifdef arm}
  195. ,raarmgas
  196. {$endif arm}
  197. {$ifdef SPARC}
  198. ,racpugas
  199. {$endif SPARC}
  200. ;
  201. function Compile(const cmd:string):longint;
  202. implementation
  203. uses
  204. {$IFDEF USE_SYSUTILS}
  205. SysUtils,
  206. {$ENDIF USE_SYSUTILS}
  207. aasmcpu;
  208. {$ifdef EXTDEBUG}
  209. {$define SHOWUSEDMEM}
  210. {$endif}
  211. {$ifdef MEMDEBUG}
  212. {$define SHOWUSEDMEM}
  213. {$endif}
  214. var
  215. CompilerInitedAfterArgs,
  216. CompilerInited : boolean;
  217. olddo_stop : tstopprocedure;
  218. {$ifdef USEEXCEPT}
  219. procedure RecoverStop(err:longint);
  220. begin
  221. if recoverpospointer<>nil then
  222. LongJmp(recoverpospointer^,1)
  223. else
  224. Stop(err);
  225. end;
  226. {$endif USEEXCEPT}
  227. {****************************************************************************
  228. Compiler
  229. ****************************************************************************}
  230. procedure DoneCompiler;
  231. begin
  232. if not CompilerInited then
  233. exit;
  234. { Free compiler if args are read }
  235. {$ifdef BrowserLog}
  236. DoneBrowserLog;
  237. {$endif BrowserLog}
  238. {$ifdef BrowserCol}
  239. do_doneSymbolInfo;
  240. {$endif BrowserCol}
  241. if CompilerInitedAfterArgs then
  242. begin
  243. CompilerInitedAfterArgs:=false;
  244. DoneParser;
  245. DoneImport;
  246. DoneExport;
  247. DoneLinker;
  248. DoneAssembler;
  249. DoneAsm;
  250. end;
  251. { Free memory for the others }
  252. CompilerInited:=false;
  253. DoneSymtable;
  254. DoneGlobals;
  255. donetokens;
  256. {$ifdef USEEXCEPT}
  257. recoverpospointer:=nil;
  258. longjump_used:=false;
  259. {$endif USEEXCEPT}
  260. end;
  261. procedure InitCompiler(const cmd:string);
  262. begin
  263. if CompilerInited then
  264. DoneCompiler;
  265. { inits which need to be done before the arguments are parsed }
  266. InitSystems;
  267. { globals depends on source_info so it must be after systems }
  268. InitGlobals;
  269. { verbose depends on exe_path and must be after globals }
  270. InitVerbose;
  271. {$ifdef BrowserLog}
  272. InitBrowserLog;
  273. {$endif BrowserLog}
  274. {$ifdef BrowserCol}
  275. do_initSymbolInfo;
  276. {$endif BrowserCol}
  277. inittokens;
  278. InitSymtable;
  279. CompilerInited:=true;
  280. { this is needed here for the IDE
  281. in case of compilation failure
  282. at the previous compile }
  283. current_module:=nil;
  284. { read the arguments }
  285. read_arguments(cmd);
  286. { inits which depend on arguments }
  287. InitParser;
  288. InitImport;
  289. InitExport;
  290. InitLinker;
  291. InitAssembler;
  292. InitAsm;
  293. CompilerInitedAfterArgs:=true;
  294. end;
  295. procedure minimal_stop(err:longint);
  296. begin
  297. DoneCompiler;
  298. olddo_stop{$ifdef FPCPROCVAR}(err){$endif};
  299. end;
  300. function Compile(const cmd:string):longint;
  301. {$ifdef fpc}
  302. {$maxfpuregisters 0}
  303. {$endif fpc}
  304. procedure writepathlist(w:longint;l:TSearchPathList);
  305. var
  306. hp : tstringlistitem;
  307. begin
  308. hp:=tstringlistitem(l.first);
  309. while assigned(hp) do
  310. begin
  311. Message1(w,hp.str);
  312. hp:=tstringlistitem(hp.next);
  313. end;
  314. end;
  315. function getrealtime : real;
  316. var
  317. {$IFDEF USE_SYSUTILS}
  318. h,m,s,s1000 : word;
  319. {$ELSE USE_SYSUTILS}
  320. h,m,s,s100 : word;
  321. {$ENDIF USE_SYSUTILS}
  322. begin
  323. {$IFDEF USE_SYSUTILS}
  324. DecodeTime(Time,h,m,s,s1000);
  325. getrealtime:=h*3600.0+m*60.0+s+s1000/1000.0;
  326. {$ELSE USE_SYSUTILS}
  327. gettime(h,m,s,s100);
  328. getrealtime:=h*3600.0+m*60.0+s+s100/100.0;
  329. {$ENDIF USE_SYSUTILS}
  330. end;
  331. var
  332. starttime : real;
  333. {$ifdef USEEXCEPT}
  334. recoverpos : jmp_buf;
  335. {$endif}
  336. begin
  337. olddo_stop:=do_stop;
  338. do_stop:={$ifdef FPCPROCVAR}@{$endif}minimal_stop;
  339. { Initialize the compiler }
  340. InitCompiler(cmd);
  341. { show some info }
  342. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  343. Message1(general_d_sourceos,source_info.name);
  344. Message1(general_i_targetos,target_info.name);
  345. Message1(general_t_exepath,exepath);
  346. WritePathList(general_t_unitpath,unitsearchpath);
  347. WritePathList(general_t_includepath,includesearchpath);
  348. WritePathList(general_t_librarypath,librarysearchpath);
  349. WritePathList(general_t_objectpath,objectsearchpath);
  350. {$ifdef USEEXCEPT}
  351. if setjmp(recoverpos)=0 then
  352. begin
  353. recoverpospointer:=@recoverpos;
  354. do_stop:={$ifdef FPCPROCVAR}@{$endif}recoverstop;
  355. {$endif USEEXCEPT}
  356. starttime:=getrealtime;
  357. {$ifdef PREPROCWRITE}
  358. if parapreprocess then
  359. parser.preprocess(inputdir+inputfile+inputextension)
  360. else
  361. {$endif PREPROCWRITE}
  362. parser.compile(inputdir+inputfile+inputextension);
  363. if status.errorcount=0 then
  364. begin
  365. starttime:=getrealtime-starttime;
  366. if starttime<0 then
  367. starttime:=starttime+3600.0*24.0;
  368. Message2(general_i_abslines_compiled,tostr(status.compiledlines),tostr(trunc(starttime))+
  369. '.'+tostr(trunc(frac(starttime)*10)));
  370. end;
  371. {$ifdef USEEXCEPT}
  372. end;
  373. {$endif USEEXCEPT}
  374. { Stop is always called, so we come here when a program is compiled or not }
  375. do_stop:=olddo_stop;
  376. { Stop the compiler, frees also memory }
  377. { no message possible after this !! }
  378. DoneCompiler;
  379. { Set the return value if an error has occurred }
  380. if status.errorcount=0 then
  381. Compile:=0
  382. else
  383. Compile:=1;
  384. DoneVerbose;
  385. {$ifdef SHOWUSEDMEM}
  386. Writeln('Memory used (heapsize): ',DStr(system.Heapsize shr 10),' Kb');
  387. {$endif SHOWUSEDMEM}
  388. {$ifdef fixLeaksOnError}
  389. do_stop{$ifdef FPCPROCVAR}(){$endif};
  390. {$endif fixLeaksOnError}
  391. end;
  392. end.
  393. {
  394. $Log$
  395. Revision 1.48 2004-10-14 17:17:25 mazen
  396. * use SysUtils unit instead of Dos Unit
  397. Revision 1.47 2004/09/08 11:23:31 michael
  398. + Check if outputdir exists, Fix exitcode when displaying help pages
  399. Revision 1.46 2004/09/04 21:18:47 armin
  400. * target netwlibc added (libc is preferred for newer netware versions)
  401. Revision 1.45 2004/06/20 08:55:29 florian
  402. * logs truncated
  403. Revision 1.44 2004/01/14 23:39:05 florian
  404. * another bunch of x86-64 fixes mainly calling convention and
  405. assembler reader related
  406. }