switches.pas 13 KB

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