switches.pas 13 KB

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