compiler.pas 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. {
  2. This unit is the interface of the compiler which can be used by
  3. external programs to link in the compiler
  4. Copyright (c) 1998-2005 by Florian Klaempfl
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************}
  17. unit compiler;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. {$ifdef GO32V2}
  22. emu387,
  23. {$endif GO32V2}
  24. {$ifdef WATCOM}
  25. emu387,
  26. {$endif WATCOM}
  27. {$IFNDEF USE_FAKE_SYSUTILS}
  28. sysutils,math,
  29. {$ELSE}
  30. fksysutl,
  31. {$ENDIF}
  32. verbose,comphook,systems,
  33. cutils,cfileutl,cclasses,globals,options,fmodule,parser,symtable,
  34. assemble,link,dbgbase,import,export,tokens,pass_1
  35. { cpu parameter handling }
  36. ,cpupara
  37. { procinfo stuff }
  38. ,cpupi
  39. { cpu codegenerator }
  40. ,cgcpu
  41. {$ifndef NOPASS2}
  42. ,cpunode
  43. {$endif}
  44. { cpu targets }
  45. ,cputarg
  46. { system information for source system }
  47. { the information about the target os }
  48. { are pulled in by the t_* units }
  49. {$ifdef amiga}
  50. ,i_amiga
  51. {$endif amiga}
  52. {$ifdef atari}
  53. ,i_atari
  54. {$endif atari}
  55. {$ifdef beos}
  56. ,i_beos
  57. {$endif beos}
  58. {$ifdef fbsd}
  59. ,i_fbsd
  60. {$endif fbsd}
  61. {$ifdef gba}
  62. ,i_gba
  63. {$endif gba}
  64. {$ifdef go32v2}
  65. ,i_go32v2
  66. {$endif go32v2}
  67. {$ifdef linux}
  68. ,i_linux
  69. {$endif linux}
  70. {$ifdef macos}
  71. ,i_macos
  72. {$endif macos}
  73. {$ifdef nds}
  74. ,i_nds
  75. {$endif nds}
  76. {$ifdef nwm}
  77. ,i_nwm
  78. {$endif nwm}
  79. {$ifdef nwl}
  80. ,i_nwl
  81. {$endif nwm}
  82. {$ifdef os2}
  83. {$ifdef emx}
  84. ,i_emx
  85. {$else emx}
  86. ,i_os2
  87. {$endif emx}
  88. {$endif os2}
  89. {$ifdef palmos}
  90. ,i_palmos
  91. {$endif palmos}
  92. {$ifdef solaris}
  93. ,i_sunos
  94. {$endif solaris}
  95. {$ifdef wdosx}
  96. ,i_wdosx
  97. {$endif wdosx}
  98. {$ifdef win32}
  99. ,i_win
  100. {$endif win32}
  101. {$ifdef symbian}
  102. ,i_symbian
  103. {$endif symbian}
  104. ;
  105. function Compile(const cmd:string):longint;
  106. implementation
  107. uses
  108. aasmcpu;
  109. {$if defined(EXTDEBUG) or defined(MEMDEBUG)}
  110. {$define SHOWUSEDMEM}
  111. {$endif}
  112. var
  113. CompilerInitedAfterArgs,
  114. CompilerInited : boolean;
  115. {****************************************************************************
  116. Compiler
  117. ****************************************************************************}
  118. procedure DoneCompiler;
  119. begin
  120. if not CompilerInited then
  121. exit;
  122. { Free compiler if args are read }
  123. if CompilerInitedAfterArgs then
  124. begin
  125. CompilerInitedAfterArgs:=false;
  126. DoneParser;
  127. DoneImport;
  128. DoneExport;
  129. DoneDebuginfo;
  130. DoneLinker;
  131. DoneAssembler;
  132. DoneAsm;
  133. end;
  134. { Free memory for the others }
  135. CompilerInited:=false;
  136. do_doneSymbolInfo;
  137. DoneSymtable;
  138. DoneGlobals;
  139. DoneFileUtils;
  140. donetokens;
  141. end;
  142. procedure InitCompiler(const cmd:string);
  143. begin
  144. if CompilerInited then
  145. DoneCompiler;
  146. { inits which need to be done before the arguments are parsed }
  147. InitSystems;
  148. { fileutils depends on source_info so it must be after systems }
  149. InitFileUtils;
  150. { globals depends on source_info so it must be after systems }
  151. InitGlobals;
  152. { verbose depends on exe_path and must be after globals }
  153. InitVerbose;
  154. inittokens;
  155. IniTSymtable; {Must come before read_arguments, to enable macrosymstack}
  156. do_initSymbolInfo;
  157. CompilerInited:=true;
  158. { this is needed here for the IDE
  159. in case of compilation failure
  160. at the previous compile }
  161. set_current_module(nil);
  162. { read the arguments }
  163. read_arguments(cmd);
  164. { inits which depend on arguments }
  165. InitParser;
  166. InitImport;
  167. InitExport;
  168. InitLinker;
  169. InitAssembler;
  170. InitDebugInfo;
  171. InitAsm;
  172. CompilerInitedAfterArgs:=true;
  173. end;
  174. function Compile(const cmd:string):longint;
  175. {$maxfpuregisters 0}
  176. procedure writepathlist(w:longint;l:TSearchPathList);
  177. var
  178. hp : TCmdStrListItem;
  179. begin
  180. hp:=TCmdStrListItem(l.first);
  181. while assigned(hp) do
  182. begin
  183. Message1(w,hp.str);
  184. hp:=TCmdStrListItem(hp.next);
  185. end;
  186. end;
  187. var
  188. timestr : string[20];
  189. linkstr : string[64];
  190. {$ifdef SHOWUSEDMEM}
  191. hstatus : TFPCHeapStatus;
  192. {$endif SHOWUSEDMEM}
  193. ExceptionMask : TFPUExceptionMask;
  194. totaltime : real;
  195. begin
  196. try
  197. try
  198. ExceptionMask:=GetExceptionMask;
  199. SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
  200. exOverflow, exUnderflow, exPrecision]);
  201. starttime:=getrealtime;
  202. { Initialize the compiler }
  203. InitCompiler(cmd);
  204. { show some info }
  205. Message1(general_t_compilername,FixFileName(system.paramstr(0)));
  206. Message1(general_d_sourceos,source_info.name);
  207. Message1(general_i_targetos,target_info.name);
  208. Message1(general_t_exepath,exepath);
  209. WritePathList(general_t_unitpath,unitsearchpath);
  210. WritePathList(general_t_includepath,includesearchpath);
  211. WritePathList(general_t_librarypath,librarysearchpath);
  212. WritePathList(general_t_objectpath,objectsearchpath);
  213. { Compile the program }
  214. {$ifdef PREPROCWRITE}
  215. if parapreprocess then
  216. parser.preprocess(inputfilepath+inputfilename)
  217. else
  218. {$endif PREPROCWRITE}
  219. parser.compile(inputfilepath+inputfilename);
  220. { Show statistics }
  221. if status.errorcount=0 then
  222. begin
  223. totaltime:=getrealtime-starttime;
  224. if totaltime<0 then
  225. totaltime:=totaltime+3600.0*24.0;
  226. timestr:=tostr(trunc(totaltime))+'.'+tostr(round(frac(totaltime)*10));
  227. if status.codesize<>-1 then
  228. linkstr:=', '+tostr(status.codesize)+' ' +strpas(MessagePChar(general_text_bytes_code))+', '+tostr(status.datasize)+' '+strpas(MessagePChar(general_text_bytes_data))
  229. else
  230. linkstr:='';
  231. Message3(general_i_abslines_compiled,tostr(status.compiledlines),timestr,linkstr);
  232. if (Status.Verbosity and V_Warning = V_Warning) and
  233. (Status.CountWarnings <> 0) then
  234. Message1 (general_i_number_of_warnings, tostr (Status.CountWarnings));
  235. if (Status.Verbosity and V_Hint = V_Hint) and
  236. (Status.CountHints <> 0) then
  237. Message1 (general_i_number_of_hints, tostr (Status.CountHints));
  238. if (Status.Verbosity and V_Note = V_Note) and
  239. (Status.CountNotes <> 0) then
  240. Message1 (general_i_number_of_notes, tostr (Status.CountNotes));
  241. end;
  242. finally
  243. { no message possible after this !! }
  244. DoneCompiler;
  245. SetExceptionMask(ExceptionMask);
  246. end;
  247. DoneVerbose;
  248. except
  249. on EControlCAbort do
  250. begin
  251. try
  252. { in case of 50 errors, this could cause another exception,
  253. suppress this exception
  254. }
  255. Message(general_f_compilation_aborted);
  256. except
  257. on ECompilerAbort do
  258. ;
  259. end;
  260. DoneVerbose;
  261. end;
  262. on ECompilerAbort do
  263. begin
  264. try
  265. { in case of 50 errors, this could cause another exception,
  266. suppress this exception
  267. }
  268. Message(general_f_compilation_aborted);
  269. except
  270. on ECompilerAbort do
  271. ;
  272. end;
  273. DoneVerbose;
  274. end;
  275. on ECompilerAbortSilent do
  276. begin
  277. DoneVerbose;
  278. end;
  279. on Exception do
  280. begin
  281. { General catchall, normally not used }
  282. try
  283. { in case of 50 errors, this could cause another exception,
  284. suppress this exception
  285. }
  286. Message(general_f_compilation_aborted);
  287. except
  288. on ECompilerAbort do
  289. ;
  290. end;
  291. DoneVerbose;
  292. Raise;
  293. end;
  294. end;
  295. {$ifdef SHOWUSEDMEM}
  296. hstatus:=GetFPCHeapStatus;
  297. Writeln('Max Memory used/heapsize: ',DStr(hstatus.MaxHeapUsed shr 10),'/',DStr(hstatus.MaxHeapSize shr 10),' Kb');
  298. {$endif SHOWUSEDMEM}
  299. { Set the return value if an error has occurred }
  300. if status.errorcount=0 then
  301. result:=0
  302. else
  303. result:=1;
  304. end;
  305. end.