switches.pas 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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_none)),
  79. {P} (typesw:modulesw; 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_none)),
  119. {P} (typesw:modulesw; 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. if state='+' then
  157. current_settings.packrecords:=4
  158. else
  159. current_settings.packrecords:=1;
  160. optimizersw :
  161. begin
  162. if state='+' then
  163. current_settings.optimizerswitches:=level2optimizerswitches
  164. else
  165. current_settings.optimizerswitches:=[];
  166. end;
  167. ignoredsw :
  168. Message1(scan_n_ignored_switch,'$'+switch);
  169. illegalsw :
  170. Message1(scan_w_illegal_switch,'$'+switch);
  171. unsupportedsw :
  172. Message1(scan_w_unsupported_switch,'$'+switch);
  173. localsw :
  174. recordpendinglocalswitch(tlocalswitch(setsw),state);
  175. modulesw :
  176. begin
  177. if current_module.in_global then
  178. begin
  179. {$ifndef cpufpemu}
  180. if tmoduleswitch(setsw)=cs_fp_emulation then
  181. begin
  182. Message1(scan_w_unsupported_switch_by_target,'$'+switch);
  183. end
  184. else
  185. {$endif cpufpemu}
  186. begin
  187. if state='+' then
  188. include(current_settings.moduleswitches,tmoduleswitch(setsw))
  189. else
  190. begin
  191. { Turning off debuginfo when lineinfo is requested
  192. is not possible }
  193. if not((cs_use_lineinfo in current_settings.globalswitches) and
  194. (tmoduleswitch(setsw)=cs_debuginfo)) then
  195. exclude(current_settings.moduleswitches,tmoduleswitch(setsw));
  196. end;
  197. end;
  198. end
  199. else
  200. Message(scan_w_switch_is_global);
  201. end;
  202. globalsw :
  203. begin
  204. if current_module.in_global and (current_module=main_module) then
  205. begin
  206. if state='+' then
  207. include(current_settings.globalswitches,tglobalswitch(setsw))
  208. else
  209. exclude(current_settings.globalswitches,tglobalswitch(setsw));
  210. end
  211. else
  212. Message(scan_w_switch_is_global);
  213. end;
  214. packenumsw:
  215. begin
  216. if state='-' then
  217. current_settings.packenum:=1
  218. else
  219. current_settings.packenum:=4;
  220. end;
  221. pentiumfdivsw:
  222. begin
  223. { Switch u- means pentium-safe fdiv off -> fpc default. We don't }
  224. { support u+ }
  225. if state='+' then
  226. Message1(scan_w_unsupported_switch,'$'+switch);
  227. end;
  228. targetsw:
  229. UpdateTargetSwitchStr(TargetSwitchStr[ttargetswitch(setsw)].name+state,current_settings.targetswitches,current_module.in_global);
  230. end;
  231. end;
  232. end;
  233. function CheckSwitch(switch,state:char):boolean;
  234. var
  235. found : boolean;
  236. switchTablePtr: ^SwitchRecTable;
  237. begin
  238. switch:=upcase(switch);
  239. { Is the Switch in the letters ? }
  240. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  241. begin
  242. Message(scan_w_illegal_switch);
  243. CheckSwitch:=false;
  244. exit;
  245. end;
  246. { Select switch table }
  247. if m_mac in current_settings.modeswitches then
  248. switchTablePtr:= @macSwitchTable
  249. else
  250. switchTablePtr:= @turboSwitchTable;
  251. { Check the switch }
  252. with switchTablePtr^[switch] do
  253. begin
  254. case typesw of
  255. localsw : found:=(tlocalswitch(setsw) in current_settings.localswitches);
  256. modulesw : found:=(tmoduleswitch(setsw) in current_settings.moduleswitches);
  257. globalsw : found:=(tglobalswitch(setsw) in current_settings.globalswitches);
  258. packenumsw : found := (current_settings.packenum = 4);
  259. else
  260. found:=false;
  261. end;
  262. if state='-' then
  263. found:=not found;
  264. CheckSwitch:=found;
  265. end;
  266. end;
  267. procedure recordpendingverbosityswitch(sw: char; state: char);
  268. begin
  269. pendingstate.nextverbositystr:=pendingstate.nextverbositystr+sw+state;
  270. end;
  271. procedure recordpendingmessagestate(msg: longint; state: tmsgstate);
  272. var
  273. pstate : pmessagestaterecord;
  274. begin
  275. new(pstate);
  276. pstate^.next:=pendingstate.nextmessagerecord;
  277. pstate^.value:=msg;
  278. pstate^.state:=state;
  279. pendingstate.nextmessagerecord:=pstate;
  280. end;
  281. procedure recordpendinglocalswitch(sw: tlocalswitch; state: char);
  282. begin
  283. if not (psf_local_switches_changed in pendingstate.flags) then
  284. pendingstate.nextlocalswitches:=current_settings.localswitches;
  285. if state='-' then
  286. exclude(pendingstate.nextlocalswitches,sw)
  287. else if state='+' then
  288. include(pendingstate.nextlocalswitches,sw)
  289. else { state = '*' }
  290. begin
  291. if sw in init_settings.localswitches then
  292. include(pendingstate.nextlocalswitches,sw)
  293. else
  294. exclude(pendingstate.nextlocalswitches,sw);
  295. end;
  296. include(pendingstate.flags,psf_local_switches_changed);
  297. end;
  298. procedure recordpendingalignmentfullswitch(const alignment : talignmentinfo);
  299. begin
  300. pendingstate.nextalignment:=alignment;
  301. include(pendingstate.flags,psf_alignment_changed);
  302. end;
  303. procedure recordpendinglocalfullswitch(const switches: tlocalswitches);
  304. begin
  305. pendingstate.nextlocalswitches:=switches;
  306. include(pendingstate.flags,psf_local_switches_changed);
  307. end;
  308. procedure recordpendingverbosityfullswitch(verbosity: longint);
  309. begin
  310. pendingstate.nextverbositystr:='';
  311. pendingstate.nextverbosityfullswitch:=verbosity;
  312. include(pendingstate.flags,psf_verbosity_full_switched);
  313. end;
  314. procedure recordpendingcallingswitch(const str: shortstring);
  315. begin
  316. pendingstate.nextcallingstr:=str;
  317. end;
  318. procedure recordpendingsetalloc(alloc:shortint);
  319. begin
  320. pendingstate.nextsetalloc:=alloc;
  321. include(pendingstate.flags,psf_setalloc_changed);
  322. end;
  323. procedure recordpendingpackenum(size:shortint);
  324. begin
  325. pendingstate.nextpackenum:=size;
  326. include(pendingstate.flags,psf_packenum_changed);
  327. end;
  328. procedure recordpendingpackrecords(size:shortint);
  329. begin
  330. pendingstate.nextpackrecords:=size;
  331. include(pendingstate.flags,psf_packrecords_changed);
  332. end;
  333. procedure flushpendingswitchesstate;
  334. var
  335. tmpproccal: tproccalloption;
  336. fstate, pstate : pmessagestaterecord;
  337. begin
  338. { process pending localswitches (range checking, etc) }
  339. if psf_local_switches_changed in pendingstate.flags then
  340. begin
  341. current_settings.localswitches:=pendingstate.nextlocalswitches;
  342. exclude(pendingstate.flags,psf_local_switches_changed);
  343. end;
  344. { process pending verbosity changes (warnings on, etc) }
  345. if psf_verbosity_full_switched in pendingstate.flags then
  346. begin
  347. status.verbosity:=pendingstate.nextverbosityfullswitch;
  348. exclude(pendingstate.flags,psf_verbosity_full_switched);
  349. end;
  350. if psf_alignment_changed in pendingstate.flags then
  351. begin
  352. current_settings.alignment:=pendingstate.nextalignment;
  353. exclude(pendingstate.flags,psf_alignment_changed);
  354. end;
  355. if psf_packenum_changed in pendingstate.flags then
  356. begin
  357. current_settings.packenum:=pendingstate.nextpackenum;
  358. exclude(pendingstate.flags,psf_packenum_changed);
  359. end;
  360. if psf_packrecords_changed in pendingstate.flags then
  361. begin
  362. current_settings.packrecords:=pendingstate.nextpackrecords;
  363. exclude(pendingstate.flags,psf_packrecords_changed);
  364. end;
  365. if psf_setalloc_changed in pendingstate.flags then
  366. begin
  367. current_settings.setalloc:=pendingstate.nextsetalloc;
  368. exclude(pendingstate.flags,psf_setalloc_changed);
  369. end;
  370. { process pending verbosity changes (warnings on, etc) }
  371. if pendingstate.nextverbositystr<>'' then
  372. begin
  373. setverbosity(pendingstate.nextverbositystr);
  374. pendingstate.nextverbositystr:='';
  375. end;
  376. fstate:=pendingstate.nextmessagerecord;
  377. pstate:=pendingstate.nextmessagerecord;
  378. while assigned(pstate) do
  379. begin
  380. pendingstate.nextmessagerecord:=pstate^.next;
  381. SetMessageVerbosity(pstate^.value,pstate^.state);
  382. if not assigned(pstate^.next) then
  383. begin
  384. pstate^.next:=current_settings.pmessage;
  385. current_settings.pmessage:=fstate;
  386. pstate:=nil;
  387. end
  388. else
  389. pstate:=pstate^.next;
  390. pendingstate.nextmessagerecord:=nil;
  391. end;
  392. { process pending calling convention changes (calling x) }
  393. if pendingstate.nextcallingstr<>'' then
  394. begin
  395. if not SetAktProcCall(pendingstate.nextcallingstr,tmpproccal) then
  396. Message1(parser_w_unknown_proc_directive_ignored,pendingstate.nextcallingstr)
  397. else if not(tmpproccal in supported_calling_conventions) then
  398. Message1(parser_e_illegal_calling_convention,pendingstate.nextcallingstr)
  399. else
  400. current_settings.defproccall:=tmpproccal;
  401. pendingstate.nextcallingstr:='';
  402. end;
  403. end;
  404. end.