switches.pas 16 KB

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