switches.pas 13 KB

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