switches.pas 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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 recordpendingasmmode(asmmode:tasmmode);
  35. procedure flushpendingswitchesstate;
  36. implementation
  37. uses
  38. cpuinfo,
  39. {$ifdef llvm}
  40. { override optimizer switches }
  41. llvminfo,
  42. {$endif llvm}
  43. globals,verbose,comphook,dirparse,cclasses,
  44. fmodule;
  45. {****************************************************************************
  46. Main Switches Parsing
  47. ****************************************************************************}
  48. type
  49. TSwitchType=(ignoredsw,localsw,modulesw,globalsw,illegalsw,unsupportedsw,alignsw,optimizersw,packenumsw,pentiumfdivsw,targetsw);
  50. SwitchRec=record
  51. typesw : TSwitchType;
  52. setsw : byte;
  53. end;
  54. SwitchRecTable = array['A'..'Z'] of SwitchRec;
  55. const
  56. turboSwitchTable: SwitchRecTable =(
  57. {A} (typesw:alignsw; setsw:ord(cs_localnone)),
  58. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  59. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  60. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  61. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  62. {$ifdef i8086}
  63. {F} (typesw:localsw; setsw:ord(cs_force_far_calls)),
  64. {$else i8086}
  65. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  66. {$endif i8086}
  67. {G} (typesw:localsw; setsw:ord(cs_imported_data)),
  68. {H} (typesw:localsw; setsw:ord(cs_refcountedstrings)),
  69. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  70. {J} (typesw:localsw; setsw:ord(cs_typed_const_writable)),
  71. {$ifdef i8086}
  72. {K} (typesw:modulesw; setsw:ord(cs_win16_smartcallbacks)),
  73. {$else i8086}
  74. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  75. {$endif i8086}
  76. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  77. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  78. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  79. {O} (typesw:optimizersw; setsw:ord(cs_opt_level2)),
  80. {P} (typesw:localsw; setsw:ord(cs_openstring)),
  81. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  82. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  83. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  84. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  85. {U} (typesw:pentiumfdivsw; setsw:ord(cs_localnone)),
  86. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  87. {$ifdef i8086}
  88. {W} (typesw:targetsw; setsw:ord(ts_x86_far_procs_push_odd_bp)),
  89. {$else i8086}
  90. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  91. {$endif i8086}
  92. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  93. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  94. {Z} (typesw:packenumsw; setsw:ord(cs_localnone))
  95. );
  96. macSwitchTable: SwitchRecTable =(
  97. {A} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  98. {B} (typesw:localsw; setsw:ord(cs_full_boolean_eval)),
  99. {C} (typesw:localsw; setsw:ord(cs_do_assertion)),
  100. {D} (typesw:modulesw; setsw:ord(cs_debuginfo)),
  101. {E} (typesw:modulesw; setsw:ord(cs_fp_emulation)),
  102. {$ifdef i8086}
  103. {F} (typesw:localsw; setsw:ord(cs_force_far_calls)),
  104. {$else i8086}
  105. {F} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  106. {$endif i8086}
  107. {G} (typesw:ignoredsw; setsw:ord(cs_localnone)),
  108. {H} (typesw:localsw; setsw:ord(cs_refcountedstrings)),
  109. {I} (typesw:localsw; setsw:ord(cs_check_io)),
  110. {J} (typesw:localsw; setsw:ord(cs_external_var)),
  111. {$ifdef i8086}
  112. {K} (typesw:modulesw; setsw:ord(cs_win16_smartcallbacks)),
  113. {$else i8086}
  114. {K} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  115. {$endif i8086}
  116. {L} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  117. {M} (typesw:localsw; setsw:ord(cs_generate_rtti)),
  118. {N} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  119. {O} (typesw:optimizersw; setsw:ord(cs_opt_level2)),
  120. {P} (typesw:localsw; setsw:ord(cs_openstring)),
  121. {Q} (typesw:localsw; setsw:ord(cs_check_overflow)),
  122. {R} (typesw:localsw; setsw:ord(cs_check_range)),
  123. {S} (typesw:localsw; setsw:ord(cs_check_stack)),
  124. {T} (typesw:localsw; setsw:ord(cs_typed_addresses)),
  125. {U} (typesw:illegalsw; setsw:ord(cs_localnone)),
  126. {V} (typesw:localsw; setsw:ord(cs_strict_var_strings)),
  127. {$ifdef i8086}
  128. {W} (typesw:targetsw; setsw:ord(ts_x86_far_procs_push_odd_bp)),
  129. {$else i8086}
  130. {W} (typesw:localsw; setsw:ord(cs_generate_stackframes)),
  131. {$endif i8086}
  132. {X} (typesw:modulesw; setsw:ord(cs_extsyntax)),
  133. {Y} (typesw:unsupportedsw; setsw:ord(cs_localnone)),
  134. {Z} (typesw:localsw; setsw:ord(cs_externally_visible))
  135. );
  136. procedure HandleSwitch(switch,state:char);
  137. var
  138. switchTablePtr: ^SwitchRecTable;
  139. begin
  140. switch:=upcase(switch);
  141. { Is the Switch in the letters ? }
  142. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  143. begin
  144. Message(scan_w_illegal_switch);
  145. exit;
  146. end;
  147. { Select switch table }
  148. if m_mac in current_settings.modeswitches then
  149. switchTablePtr:= @macSwitchTable
  150. else
  151. switchTablePtr:= @turboSwitchTable;
  152. { Handle the switch }
  153. with switchTablePtr^[switch] do
  154. begin
  155. case typesw of
  156. alignsw:
  157. begin
  158. if state='+' then
  159. recordpendingpackrecords(4)
  160. else
  161. recordpendingpackrecords(1);
  162. end;
  163. optimizersw :
  164. begin
  165. if state='+' then
  166. current_settings.optimizerswitches:=level2optimizerswitches
  167. else
  168. current_settings.optimizerswitches:=[];
  169. end;
  170. ignoredsw :
  171. Message1(scan_n_ignored_switch,'$'+switch);
  172. illegalsw :
  173. Message1(scan_w_illegal_switch,'$'+switch);
  174. unsupportedsw :
  175. Message1(scan_w_unsupported_switch,'$'+switch);
  176. localsw :
  177. recordpendinglocalswitch(tlocalswitch(setsw),state);
  178. modulesw :
  179. begin
  180. if current_module.in_global then
  181. begin
  182. {$ifndef cpufpemu}
  183. if tmoduleswitch(setsw)=cs_fp_emulation then
  184. begin
  185. Message1(scan_w_unsupported_switch_by_target,'$'+switch);
  186. end
  187. else
  188. {$endif cpufpemu}
  189. begin
  190. if state='+' then
  191. include(current_settings.moduleswitches,tmoduleswitch(setsw))
  192. else
  193. begin
  194. { Turning off debuginfo when lineinfo is requested
  195. is not possible }
  196. if not((cs_use_lineinfo in current_settings.globalswitches) and
  197. (tmoduleswitch(setsw)=cs_debuginfo)) then
  198. exclude(current_settings.moduleswitches,tmoduleswitch(setsw));
  199. end;
  200. end;
  201. end
  202. else
  203. Message(scan_w_switch_is_global);
  204. end;
  205. globalsw :
  206. begin
  207. if current_module.in_global and (current_module=main_module) then
  208. begin
  209. if state='+' then
  210. include(current_settings.globalswitches,tglobalswitch(setsw))
  211. else
  212. exclude(current_settings.globalswitches,tglobalswitch(setsw));
  213. end
  214. else
  215. Message(scan_w_switch_is_global);
  216. end;
  217. packenumsw:
  218. begin
  219. if state='-' then
  220. current_settings.packenum:=1
  221. else
  222. current_settings.packenum:=4;
  223. end;
  224. pentiumfdivsw:
  225. begin
  226. { Switch u- means pentium-safe fdiv off -> fpc default. We don't }
  227. { support u+ }
  228. if state='+' then
  229. Message1(scan_w_unsupported_switch,'$'+switch);
  230. end;
  231. targetsw:
  232. UpdateTargetSwitchStr(TargetSwitchStr[ttargetswitch(setsw)].name+state,current_settings.targetswitches,current_module.in_global);
  233. end;
  234. end;
  235. end;
  236. function CheckSwitch(switch,state:char):boolean;
  237. var
  238. found : boolean;
  239. switchTablePtr: ^SwitchRecTable;
  240. begin
  241. switch:=upcase(switch);
  242. { Is the Switch in the letters ? }
  243. if not ((switch in ['A'..'Z']) and (state in ['-','+'])) then
  244. begin
  245. Message(scan_w_illegal_switch);
  246. CheckSwitch:=false;
  247. exit;
  248. end;
  249. { Select switch table }
  250. if m_mac in current_settings.modeswitches then
  251. switchTablePtr:= @macSwitchTable
  252. else
  253. switchTablePtr:= @turboSwitchTable;
  254. { Check the switch }
  255. with switchTablePtr^[switch] do
  256. begin
  257. case typesw of
  258. localsw : found:=(tlocalswitch(setsw) in current_settings.localswitches);
  259. modulesw : found:=(tmoduleswitch(setsw) in current_settings.moduleswitches);
  260. globalsw : found:=(tglobalswitch(setsw) in current_settings.globalswitches);
  261. packenumsw : found := (current_settings.packenum = 4);
  262. else
  263. found:=false;
  264. end;
  265. if state='-' then
  266. found:=not found;
  267. CheckSwitch:=found;
  268. end;
  269. end;
  270. procedure recordpendingverbosityswitch(sw: char; state: char);
  271. begin
  272. pendingstate.nextverbositystr:=pendingstate.nextverbositystr+sw+state;
  273. end;
  274. procedure recordpendingmessagestate(msg: longint; state: tmsgstate);
  275. var
  276. pstate : pmessagestaterecord;
  277. begin
  278. new(pstate);
  279. pstate^.next:=pendingstate.nextmessagerecord;
  280. pstate^.value:=msg;
  281. pstate^.state:=state;
  282. pendingstate.nextmessagerecord:=pstate;
  283. end;
  284. procedure recordpendinglocalswitch(sw: tlocalswitch; state: char);
  285. begin
  286. if not (psf_local_switches_changed in pendingstate.flags) then
  287. pendingstate.nextlocalswitches:=current_settings.localswitches;
  288. if state='-' then
  289. exclude(pendingstate.nextlocalswitches,sw)
  290. else if state='+' then
  291. include(pendingstate.nextlocalswitches,sw)
  292. else { state = '*' }
  293. begin
  294. if sw in init_settings.localswitches then
  295. include(pendingstate.nextlocalswitches,sw)
  296. else
  297. exclude(pendingstate.nextlocalswitches,sw);
  298. end;
  299. include(pendingstate.flags,psf_local_switches_changed);
  300. end;
  301. procedure recordpendingalignmentfullswitch(const alignment : talignmentinfo);
  302. begin
  303. pendingstate.nextalignment:=alignment;
  304. include(pendingstate.flags,psf_alignment_changed);
  305. end;
  306. procedure recordpendinglocalfullswitch(const switches: tlocalswitches);
  307. begin
  308. pendingstate.nextlocalswitches:=switches;
  309. include(pendingstate.flags,psf_local_switches_changed);
  310. end;
  311. procedure recordpendingverbosityfullswitch(verbosity: longint);
  312. begin
  313. pendingstate.nextverbositystr:='';
  314. pendingstate.nextverbosityfullswitch:=verbosity;
  315. include(pendingstate.flags,psf_verbosity_full_switched);
  316. end;
  317. procedure recordpendingcallingswitch(const str: shortstring);
  318. begin
  319. pendingstate.nextcallingstr:=str;
  320. end;
  321. procedure recordpendingsetalloc(alloc:shortint);
  322. begin
  323. pendingstate.nextsetalloc:=alloc;
  324. include(pendingstate.flags,psf_setalloc_changed);
  325. end;
  326. procedure recordpendingasmmode(asmmode:tasmmode);
  327. begin
  328. pendingstate.nextasmmode:=asmmode;
  329. include(pendingstate.flags,psf_asmmode_changed);
  330. end;
  331. procedure recordpendingpackenum(size:shortint);
  332. begin
  333. pendingstate.nextpackenum:=size;
  334. include(pendingstate.flags,psf_packenum_changed);
  335. end;
  336. procedure recordpendingpackrecords(size:shortint);
  337. begin
  338. pendingstate.nextpackrecords:=size;
  339. include(pendingstate.flags,psf_packrecords_changed);
  340. end;
  341. procedure flushpendingswitchesstate;
  342. var
  343. tmpproccal: tproccalloption;
  344. fstate, pstate : pmessagestaterecord;
  345. msgset : thashset;
  346. msgfound : boolean;
  347. begin
  348. { process pending localswitches (range checking, etc) }
  349. if psf_local_switches_changed in pendingstate.flags then
  350. begin
  351. current_settings.localswitches:=pendingstate.nextlocalswitches;
  352. exclude(pendingstate.flags,psf_local_switches_changed);
  353. end;
  354. { process pending verbosity changes (warnings on, etc) }
  355. if psf_verbosity_full_switched in pendingstate.flags then
  356. begin
  357. status.verbosity:=pendingstate.nextverbosityfullswitch;
  358. exclude(pendingstate.flags,psf_verbosity_full_switched);
  359. end;
  360. if psf_alignment_changed in pendingstate.flags then
  361. begin
  362. current_settings.alignment:=pendingstate.nextalignment;
  363. exclude(pendingstate.flags,psf_alignment_changed);
  364. end;
  365. if psf_packenum_changed in pendingstate.flags then
  366. begin
  367. current_settings.packenum:=pendingstate.nextpackenum;
  368. exclude(pendingstate.flags,psf_packenum_changed);
  369. end;
  370. if psf_packrecords_changed in pendingstate.flags then
  371. begin
  372. current_settings.packrecords:=pendingstate.nextpackrecords;
  373. exclude(pendingstate.flags,psf_packrecords_changed);
  374. end;
  375. if psf_setalloc_changed in pendingstate.flags then
  376. begin
  377. current_settings.setalloc:=pendingstate.nextsetalloc;
  378. exclude(pendingstate.flags,psf_setalloc_changed);
  379. end;
  380. if psf_asmmode_changed in pendingstate.flags then
  381. begin
  382. current_settings.asmmode:=pendingstate.nextasmmode;
  383. exclude(pendingstate.flags,psf_asmmode_changed);
  384. end;
  385. { process pending verbosity changes (warnings on, etc) }
  386. if pendingstate.nextverbositystr<>'' then
  387. begin
  388. setverbosity(pendingstate.nextverbositystr);
  389. pendingstate.nextverbositystr:='';
  390. end;
  391. msgset:=thashset.create(10,false,false);
  392. { we need to start from a clean slate }
  393. if not assigned(current_settings.pmessage) then
  394. RestoreLocalVerbosity(nil);
  395. fstate:=pendingstate.nextmessagerecord;
  396. pstate:=pendingstate.nextmessagerecord;
  397. while assigned(pstate) do
  398. begin
  399. pendingstate.nextmessagerecord:=pstate^.next;
  400. { the message records are ordered newest to oldest, so only apply the newest change }
  401. msgfound:=false;
  402. if not assigned(msgset.findoradd(@pstate^.value,sizeof(pstate^.value),msgfound)) or
  403. not msgfound then
  404. SetMessageVerbosity(pstate^.value,pstate^.state);
  405. if not assigned(pstate^.next) then
  406. begin
  407. pstate^.next:=current_settings.pmessage;
  408. current_settings.pmessage:=fstate;
  409. pstate:=nil;
  410. end
  411. else
  412. pstate:=pstate^.next;
  413. pendingstate.nextmessagerecord:=nil;
  414. end;
  415. msgset.free;
  416. { process pending calling convention changes (calling x) }
  417. if pendingstate.nextcallingstr<>'' then
  418. begin
  419. if not SetAktProcCall(pendingstate.nextcallingstr,tmpproccal) then
  420. Message1(parser_w_unknown_proc_directive_ignored,pendingstate.nextcallingstr)
  421. else if not(tmpproccal in supported_calling_conventions) then
  422. Message1(parser_e_illegal_calling_convention,pendingstate.nextcallingstr)
  423. else
  424. current_settings.defproccall:=tmpproccal;
  425. pendingstate.nextcallingstr:='';
  426. end;
  427. end;
  428. end.