switches.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. {
  2. Copyright (c) 1998-2002 by Peter Vreman
  3. This unit implements the parsing of the switches like $I-
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. unit switches;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. systems,globtype;
  22. procedure HandleSwitch(switch,state:char);
  23. function CheckSwitch(switch,state:char):boolean;
  24. procedure recordpendingverbosityswitch(sw: char; state: char);
  25. procedure recordpendingmessagestate(msg: longint; state: tmsgstate);
  26. procedure recordpendinglocalswitch(sw: tlocalswitch; state: char);
  27. procedure recordpendinglocalfullswitch(const switches: tlocalswitches);
  28. procedure recordpendingverbosityfullswitch(verbosity: longint);
  29. procedure recordpendingcallingswitch(const str: shortstring);
  30. procedure recordpendingalignmentfullswitch(const alignment : talignmentinfo);
  31. procedure recordpendingsetalloc(alloc:shortint);
  32. procedure recordpendingpackenum(size:shortint);
  33. procedure recordpendingpackrecords(size:shortint);
  34. procedure flushpendingswitchesstate;
  35. implementation
  36. uses
  37. cpuinfo,
  38. {$ifdef llvm}
  39. { override optimizer switches }
  40. llvminfo,
  41. {$endif llvm}
  42. globals,verbose,comphook,dirparse,
  43. fmodule;
  44. {****************************************************************************
  45. Main Switches Parsing
  46. ****************************************************************************}
  47. type
  48. TSwitchType=(ignoredsw,localsw,modulesw,globalsw,illegalsw,unsupportedsw,alignsw,optimizersw,packenumsw,pentiumfdivsw,targetsw);
  49. SwitchRec=record
  50. typesw : TSwitchType;
  51. setsw : byte;
  52. end;
  53. SwitchRecTable = array['A'..'Z'] of SwitchRec;
  54. const
  55. turboSwitchTable: SwitchRecTable =(
  56. {A} (typesw:alignsw; setsw:ord(cs_localnone)),
  57. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  58. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  59. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  60. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  61. {$ifdef i8086}
  62. {F} (typesw:localsw; setsw:ord(cs_force_far_calls)),
  63. {$else i8086}
  64. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  65. {$endif i8086}
  66. {G} (typesw:localsw; setsw:ord(cs_imported_data)),
  67. {H} (typesw:localsw; setsw:ord(cs_refcountedstrings)),
  68. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  69. {J} (typesw:localsw; setsw:ord(cs_typed_const_writable)),
  70. {$ifdef i8086}
  71. {K} (typesw:modulesw; setsw:ord(cs_win16_smartcallbacks)),
  72. {$else i8086}
  73. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  74. {$endif i8086}
  75. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  76. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  77. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  78. {O} (typesw:optimizersw; setsw:ord(cs_opt_level2)),
  79. {P} (typesw:localsw; setsw:ord(cs_openstring)),
  80. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  81. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  82. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  83. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  84. {U} (typesw:pentiumfdivsw; setsw:ord(cs_localnone)),
  85. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  86. {$ifdef i8086}
  87. {W} (typesw:targetsw; setsw:ord(ts_x86_far_procs_push_odd_bp)),
  88. {$else i8086}
  89. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  90. {$endif i8086}
  91. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  92. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  93. {Z} (typesw:packenumsw; setsw:ord(cs_localnone))
  94. );
  95. macSwitchTable: SwitchRecTable =(
  96. {A} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  97. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  98. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  99. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  100. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  101. {$ifdef i8086}
  102. {F} (typesw:localsw; setsw:ord(cs_force_far_calls)),
  103. {$else i8086}
  104. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  105. {$endif i8086}
  106. {G} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  107. {H} (typesw:localsw; setsw:ord(cs_refcountedstrings)),
  108. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  109. {J} (typesw:localsw; setsw:ord(cs_external_var)),
  110. {$ifdef i8086}
  111. {K} (typesw:modulesw; setsw:ord(cs_win16_smartcallbacks)),
  112. {$else i8086}
  113. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  114. {$endif i8086}
  115. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  116. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  117. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  118. {O} (typesw:optimizersw; setsw:ord(cs_opt_level2)),
  119. {P} (typesw:localsw; setsw:ord(cs_openstring)),
  120. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  121. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  122. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  123. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  124. {U} (typesw:illegalsw; setsw:ord(cs_localnone)),
  125. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  126. {$ifdef i8086}
  127. {W} (typesw:targetsw; setsw:ord(ts_x86_far_procs_push_odd_bp)),
  128. {$else i8086}
  129. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  130. {$endif i8086}
  131. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  132. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  133. {Z} (typesw:localsw; setsw:ord(cs_externally_visible))
  134. );
  135. procedure HandleSwitch(switch,state:char);
  136. var
  137. switchTablePtr: ^SwitchRecTable;
  138. begin
  139. switch:=upcase(switch);
  140. { Is the Switch in the letters ? }
  141. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  142. begin
  143. Message(scan_w_illegal_switch);
  144. exit;
  145. end;
  146. { Select switch table }
  147. if m_mac in current_settings.modeswitches then
  148. switchTablePtr:= @macSwitchTable
  149. else
  150. switchTablePtr:= @turboSwitchTable;
  151. { Handle the switch }
  152. with switchTablePtr^[switch] do
  153. begin
  154. case typesw of
  155. alignsw:
  156. begin
  157. if state='+' then
  158. recordpendingpackrecords(4)
  159. else
  160. recordpendingpackrecords(1);
  161. end;
  162. optimizersw :
  163. begin
  164. if state='+' then
  165. current_settings.optimizerswitches:=level2optimizerswitches
  166. else
  167. current_settings.optimizerswitches:=[];
  168. end;
  169. ignoredsw :
  170. Message1(scan_n_ignored_switch,'$'+switch);
  171. illegalsw :
  172. Message1(scan_w_illegal_switch,'$'+switch);
  173. unsupportedsw :
  174. Message1(scan_w_unsupported_switch,'$'+switch);
  175. localsw :
  176. recordpendinglocalswitch(tlocalswitch(setsw),state);
  177. modulesw :
  178. begin
  179. if current_module.in_global then
  180. begin
  181. {$ifndef cpufpemu}
  182. if tmoduleswitch(setsw)=cs_fp_emulation then
  183. begin
  184. Message1(scan_w_unsupported_switch_by_target,'$'+switch);
  185. end
  186. else
  187. {$endif cpufpemu}
  188. begin
  189. if state='+' then
  190. include(current_settings.moduleswitches,tmoduleswitch(setsw))
  191. else
  192. begin
  193. { Turning off debuginfo when lineinfo is requested
  194. is not possible }
  195. if not((cs_use_lineinfo in current_settings.globalswitches) and
  196. (tmoduleswitch(setsw)=cs_debuginfo)) then
  197. exclude(current_settings.moduleswitches,tmoduleswitch(setsw));
  198. end;
  199. end;
  200. end
  201. else
  202. Message(scan_w_switch_is_global);
  203. end;
  204. globalsw :
  205. begin
  206. if current_module.in_global and (current_module=main_module) then
  207. begin
  208. if state='+' then
  209. include(current_settings.globalswitches,tglobalswitch(setsw))
  210. else
  211. exclude(current_settings.globalswitches,tglobalswitch(setsw));
  212. end
  213. else
  214. Message(scan_w_switch_is_global);
  215. end;
  216. packenumsw:
  217. begin
  218. if state='-' then
  219. current_settings.packenum:=1
  220. else
  221. current_settings.packenum:=4;
  222. end;
  223. pentiumfdivsw:
  224. begin
  225. { Switch u- means pentium-safe fdiv off -> fpc default. We don't }
  226. { support u+ }
  227. if state='+' then
  228. Message1(scan_w_unsupported_switch,'$'+switch);
  229. end;
  230. targetsw:
  231. UpdateTargetSwitchStr(TargetSwitchStr[ttargetswitch(setsw)].name+state,current_settings.targetswitches,current_module.in_global);
  232. end;
  233. end;
  234. end;
  235. function CheckSwitch(switch,state:char):boolean;
  236. var
  237. found : boolean;
  238. switchTablePtr: ^SwitchRecTable;
  239. begin
  240. switch:=upcase(switch);
  241. { Is the Switch in the letters ? }
  242. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  243. begin
  244. Message(scan_w_illegal_switch);
  245. CheckSwitch:=false;
  246. exit;
  247. end;
  248. { Select switch table }
  249. if m_mac in current_settings.modeswitches then
  250. switchTablePtr:= @macSwitchTable
  251. else
  252. switchTablePtr:= @turboSwitchTable;
  253. { Check the switch }
  254. with switchTablePtr^[switch] do
  255. begin
  256. case typesw of
  257. localsw : found:=(tlocalswitch(setsw) in current_settings.localswitches);
  258. modulesw : found:=(tmoduleswitch(setsw) in current_settings.moduleswitches);
  259. globalsw : found:=(tglobalswitch(setsw) in current_settings.globalswitches);
  260. packenumsw : found := (current_settings.packenum = 4);
  261. else
  262. found:=false;
  263. end;
  264. if state='-' then
  265. found:=not found;
  266. CheckSwitch:=found;
  267. end;
  268. end;
  269. procedure recordpendingverbosityswitch(sw: char; state: char);
  270. begin
  271. pendingstate.nextverbositystr:=pendingstate.nextverbositystr+sw+state;
  272. end;
  273. procedure recordpendingmessagestate(msg: longint; state: tmsgstate);
  274. var
  275. pstate : pmessagestaterecord;
  276. begin
  277. new(pstate);
  278. pstate^.next:=pendingstate.nextmessagerecord;
  279. pstate^.value:=msg;
  280. pstate^.state:=state;
  281. pendingstate.nextmessagerecord:=pstate;
  282. end;
  283. procedure recordpendinglocalswitch(sw: tlocalswitch; state: char);
  284. begin
  285. if not (psf_local_switches_changed in pendingstate.flags) then
  286. pendingstate.nextlocalswitches:=current_settings.localswitches;
  287. if state='-' then
  288. exclude(pendingstate.nextlocalswitches,sw)
  289. else if state='+' then
  290. include(pendingstate.nextlocalswitches,sw)
  291. else { state = '*' }
  292. begin
  293. if sw in init_settings.localswitches then
  294. include(pendingstate.nextlocalswitches,sw)
  295. else
  296. exclude(pendingstate.nextlocalswitches,sw);
  297. end;
  298. include(pendingstate.flags,psf_local_switches_changed);
  299. end;
  300. procedure recordpendingalignmentfullswitch(const alignment : talignmentinfo);
  301. begin
  302. pendingstate.nextalignment:=alignment;
  303. include(pendingstate.flags,psf_alignment_changed);
  304. end;
  305. procedure recordpendinglocalfullswitch(const switches: tlocalswitches);
  306. begin
  307. pendingstate.nextlocalswitches:=switches;
  308. include(pendingstate.flags,psf_local_switches_changed);
  309. end;
  310. procedure recordpendingverbosityfullswitch(verbosity: longint);
  311. begin
  312. pendingstate.nextverbositystr:='';
  313. pendingstate.nextverbosityfullswitch:=verbosity;
  314. include(pendingstate.flags,psf_verbosity_full_switched);
  315. end;
  316. procedure recordpendingcallingswitch(const str: shortstring);
  317. begin
  318. pendingstate.nextcallingstr:=str;
  319. end;
  320. procedure recordpendingsetalloc(alloc:shortint);
  321. begin
  322. pendingstate.nextsetalloc:=alloc;
  323. include(pendingstate.flags,psf_setalloc_changed);
  324. end;
  325. procedure recordpendingpackenum(size:shortint);
  326. begin
  327. pendingstate.nextpackenum:=size;
  328. include(pendingstate.flags,psf_packenum_changed);
  329. end;
  330. procedure recordpendingpackrecords(size:shortint);
  331. begin
  332. pendingstate.nextpackrecords:=size;
  333. include(pendingstate.flags,psf_packrecords_changed);
  334. end;
  335. procedure flushpendingswitchesstate;
  336. var
  337. tmpproccal: tproccalloption;
  338. fstate, pstate : pmessagestaterecord;
  339. begin
  340. { process pending localswitches (range checking, etc) }
  341. if psf_local_switches_changed in pendingstate.flags then
  342. begin
  343. current_settings.localswitches:=pendingstate.nextlocalswitches;
  344. exclude(pendingstate.flags,psf_local_switches_changed);
  345. end;
  346. { process pending verbosity changes (warnings on, etc) }
  347. if psf_verbosity_full_switched in pendingstate.flags then
  348. begin
  349. status.verbosity:=pendingstate.nextverbosityfullswitch;
  350. exclude(pendingstate.flags,psf_verbosity_full_switched);
  351. end;
  352. if psf_alignment_changed in pendingstate.flags then
  353. begin
  354. current_settings.alignment:=pendingstate.nextalignment;
  355. exclude(pendingstate.flags,psf_alignment_changed);
  356. end;
  357. if psf_packenum_changed in pendingstate.flags then
  358. begin
  359. current_settings.packenum:=pendingstate.nextpackenum;
  360. exclude(pendingstate.flags,psf_packenum_changed);
  361. end;
  362. if psf_packrecords_changed in pendingstate.flags then
  363. begin
  364. current_settings.packrecords:=pendingstate.nextpackrecords;
  365. exclude(pendingstate.flags,psf_packrecords_changed);
  366. end;
  367. if psf_setalloc_changed in pendingstate.flags then
  368. begin
  369. current_settings.setalloc:=pendingstate.nextsetalloc;
  370. exclude(pendingstate.flags,psf_setalloc_changed);
  371. end;
  372. { process pending verbosity changes (warnings on, etc) }
  373. if pendingstate.nextverbositystr<>'' then
  374. begin
  375. setverbosity(pendingstate.nextverbositystr);
  376. pendingstate.nextverbositystr:='';
  377. end;
  378. fstate:=pendingstate.nextmessagerecord;
  379. pstate:=pendingstate.nextmessagerecord;
  380. while assigned(pstate) do
  381. begin
  382. pendingstate.nextmessagerecord:=pstate^.next;
  383. SetMessageVerbosity(pstate^.value,pstate^.state);
  384. if not assigned(pstate^.next) then
  385. begin
  386. pstate^.next:=current_settings.pmessage;
  387. current_settings.pmessage:=fstate;
  388. pstate:=nil;
  389. end
  390. else
  391. pstate:=pstate^.next;
  392. pendingstate.nextmessagerecord:=nil;
  393. end;
  394. { process pending calling convention changes (calling x) }
  395. if pendingstate.nextcallingstr<>'' then
  396. begin
  397. if not SetAktProcCall(pendingstate.nextcallingstr,tmpproccal) then
  398. Message1(parser_w_unknown_proc_directive_ignored,pendingstate.nextcallingstr)
  399. else if not(tmpproccal in supported_calling_conventions) then
  400. Message1(parser_e_illegal_calling_convention,pendingstate.nextcallingstr)
  401. else
  402. current_settings.defproccall:=tmpproccal;
  403. pendingstate.nextcallingstr:='';
  404. end;
  405. end;
  406. end.