ra386att.pas 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere and Peter Vreman
  4. Does the parsing for the AT&T styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif TP}
  21. Unit Ra386att;
  22. Interface
  23. uses
  24. tree;
  25. function assemble: ptree;
  26. Implementation
  27. Uses
  28. globtype,
  29. strings,cobjects,systems,verbose,globals,
  30. files,aasm,types,symconst,symtable,scanner,cpubase,
  31. {$ifdef NEWCG}
  32. cgbase,
  33. {$else}
  34. hcodegen,
  35. {$endif}
  36. rautils,ra386;
  37. type
  38. tasmtoken = (
  39. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  40. AS_REALNUM,AS_COMMA,AS_LPAREN,
  41. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  42. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  43. {------------------ Assembler directives --------------------}
  44. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,
  45. AS_ALIGN,AS_BALIGN,AS_P2ALIGN,AS_ASCII,
  46. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,
  47. AS_DATA,AS_TEXT,AS_END,
  48. {------------------ Assembler Operators --------------------}
  49. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR);
  50. tasmkeyword = string[10];
  51. const
  52. { These tokens should be modified accordingly to the modifications }
  53. { in the different enumerations. }
  54. firstdirective = AS_DB;
  55. lastdirective = AS_END;
  56. _count_asmprefixes = 5;
  57. _count_asmspecialops = 25;
  58. _count_asmoverrides = 3;
  59. token2str : array[tasmtoken] of tasmkeyword=(
  60. '','Label','LLabel','string','integer',
  61. 'float',',','(',
  62. ')',':','.','+','-','*',
  63. ';','identifier','register','opcode','/','$',
  64. '.byte','.word','.long','.quad','.globl',
  65. '.align','.balign','.p2align','.ascii',
  66. '.asciz','.lcomm','.comm','.single','.double','.tfloat',
  67. '.data','.text','END',
  68. '%','<<','>>','!','&','|','^','~');
  69. const
  70. newline = #10;
  71. firsttoken : boolean = TRUE;
  72. var
  73. _asmsorted : boolean;
  74. curlist : paasmoutput;
  75. c : char;
  76. actasmtoken : tasmtoken;
  77. prevasmtoken : tasmtoken;
  78. actasmpattern : string;
  79. actopcode : tasmop;
  80. actasmregister : tregister;
  81. actopsize : topsize;
  82. actcondition : tasmcond;
  83. iasmops : ^op2strtable;
  84. iasmregs : ^reg2strtable;
  85. Procedure SetupTables;
  86. { creates uppercased symbol tables for speed access }
  87. var
  88. i : tasmop;
  89. j : tregister;
  90. Begin
  91. { opcodes }
  92. new(iasmops);
  93. for i:=firstop to lastop do
  94. iasmops^[i] := upper(att_op2str[i]);
  95. { registers }
  96. new(iasmregs);
  97. for j:=firstreg to lastreg do
  98. iasmregs^[j] := upper(att_reg2str[j]);
  99. end;
  100. {---------------------------------------------------------------------}
  101. { Routines for the tokenizing }
  102. {---------------------------------------------------------------------}
  103. function is_asmopcode(const s: string):boolean;
  104. const
  105. { We need first to check the long prefixes, else we get probs
  106. with things like movsbl }
  107. att_sizesuffixstr : array[0..9] of string[2] = (
  108. '','BW','BL','WL','B','W','L','S','Q','T'
  109. );
  110. att_sizesuffix : array[0..9] of topsize = (
  111. S_NO,S_BW,S_BL,S_WL,S_B,S_W,S_L,S_FS,S_IQ,S_FX
  112. );
  113. att_sizefpusuffix : array[0..9] of topsize = (
  114. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_FL,S_FS,S_IQ,S_FX
  115. );
  116. att_sizefpuintsuffix : array[0..9] of topsize = (
  117. S_NO,S_NO,S_NO,S_NO,S_NO,S_NO,S_IL,S_IS,S_IQ,S_NO
  118. );
  119. var
  120. i : tasmop;
  121. cond : string[4];
  122. cnd : tasmcond;
  123. len,
  124. j,
  125. sufidx : longint;
  126. hid : string;
  127. Begin
  128. is_asmopcode:=FALSE;
  129. actopcode:=A_None;
  130. actcondition:=C_None;
  131. actopsize:=S_NO;
  132. { search for all possible suffixes }
  133. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  134. begin
  135. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  136. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  137. begin
  138. { here we search the entire table... }
  139. hid:=copy(s,1,len);
  140. for i:=firstop to lastop do
  141. if (length(hid) > 0) and (hid=iasmops^[i]) then
  142. begin
  143. actopcode:=i;
  144. if att_needsuffix[actopcode]=attsufFPU then
  145. actopsize:=att_sizefpusuffix[sufidx]
  146. else if att_needsuffix[actopcode]=attsufFPUint then
  147. actopsize:=att_sizefpuintsuffix[sufidx]
  148. else
  149. actopsize:=att_sizesuffix[sufidx];
  150. actasmtoken:=AS_OPCODE;
  151. is_asmopcode:=TRUE;
  152. exit;
  153. end;
  154. { not found, check condition opcodes }
  155. j:=0;
  156. while (j<CondAsmOps) do
  157. begin
  158. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  159. begin
  160. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  161. if cond<>'' then
  162. begin
  163. for cnd:=low(TasmCond) to high(TasmCond) do
  164. if Cond=Upper(cond2str[cnd]) then
  165. begin
  166. actopcode:=CondASmOp[j];
  167. if att_needsuffix[actopcode]=attsufFPU then
  168. actopsize:=att_sizefpusuffix[sufidx]
  169. else if att_needsuffix[actopcode]=attsufFPUint then
  170. actopsize:=att_sizefpuintsuffix[sufidx]
  171. else
  172. actopsize:=att_sizesuffix[sufidx];
  173. actcondition:=cnd;
  174. actasmtoken:=AS_OPCODE;
  175. is_asmopcode:=TRUE;
  176. exit;
  177. end;
  178. end;
  179. end;
  180. inc(j);
  181. end;
  182. end;
  183. end;
  184. end;
  185. Function is_asmdirective(const s: string):boolean;
  186. var
  187. i : tasmtoken;
  188. hs : string;
  189. Begin
  190. { GNU as is also not casesensitive with this }
  191. hs:=lower(s);
  192. for i:=firstdirective to lastdirective do
  193. if hs=token2str[i] then
  194. begin
  195. actasmtoken:=i;
  196. is_asmdirective:=true;
  197. exit;
  198. end;
  199. is_asmdirective:=false;
  200. end;
  201. Function is_register(const s: string):boolean;
  202. Var
  203. i : tregister;
  204. Begin
  205. actasmregister:=R_NO;
  206. for i:=firstreg to lastreg do
  207. if s=iasmregs^[i] then
  208. begin
  209. actasmtoken:=AS_REGISTER;
  210. actasmregister:=i;
  211. is_register:=true;
  212. exit;
  213. end;
  214. is_register:=false;
  215. end;
  216. Function is_locallabel(const s: string):boolean;
  217. begin
  218. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  219. end;
  220. Procedure GetToken;
  221. var
  222. len : longint;
  223. begin
  224. { save old token and reset new token }
  225. prevasmtoken:=actasmtoken;
  226. actasmtoken:=AS_NONE;
  227. { reset }
  228. actasmpattern:='';
  229. { while space and tab , continue scan... }
  230. while c in [' ',#9] do
  231. c:=current_scanner^.asmgetchar;
  232. { get token pos }
  233. if not (c in [newline,#13,'{',';']) then
  234. current_scanner^.gettokenpos;
  235. { Local Label, Label, Directive, Prefix or Opcode }
  236. if firsttoken and not(c in [newline,#13,'{',';']) then
  237. begin
  238. firsttoken:=FALSE;
  239. len:=0;
  240. { directive or local label }
  241. if c = '.' then
  242. begin
  243. inc(len);
  244. actasmpattern[len]:=c;
  245. { Let us point to the next character }
  246. c:=current_scanner^.asmgetchar;
  247. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  248. begin
  249. inc(len);
  250. actasmpattern[len]:=c;
  251. c:=current_scanner^.asmgetchar;
  252. end;
  253. actasmpattern[0]:=chr(len);
  254. { this is a local label... }
  255. if (c=':') and is_locallabel(actasmpattern) then
  256. Begin
  257. { local variables are case sensitive }
  258. actasmtoken:=AS_LLABEL;
  259. c:=current_scanner^.asmgetchar;
  260. firsttoken:=true;
  261. exit;
  262. end
  263. { must be a directive }
  264. else
  265. Begin
  266. { directives are case sensitive!! }
  267. if is_asmdirective(actasmpattern) then
  268. exit;
  269. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  270. end;
  271. end;
  272. { only opcodes and global labels are allowed now. }
  273. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  274. begin
  275. inc(len);
  276. actasmpattern[len]:=c;
  277. c:=current_scanner^.asmgetchar;
  278. end;
  279. actasmpattern[0]:=chr(len);
  280. { Label ? }
  281. if c = ':' then
  282. begin
  283. actasmtoken:=AS_LABEL;
  284. { let us point to the next character }
  285. c:=current_scanner^.asmgetchar;
  286. firsttoken:=true;
  287. exit;
  288. end;
  289. { Opcode ? }
  290. If is_asmopcode(upper(actasmpattern)) then
  291. Begin
  292. uppervar(actasmpattern);
  293. exit;
  294. end;
  295. { End of assemblerblock ? }
  296. if upper(actasmpattern) = 'END' then
  297. begin
  298. actasmtoken:=AS_END;
  299. exit;
  300. end;
  301. actasmtoken:=AS_NONE;
  302. end
  303. else { else firsttoken }
  304. { Here we must handle all possible cases }
  305. begin
  306. case c of
  307. '.' : { possiblities : - local label reference , such as in jmp @local1 }
  308. { - field of object/record }
  309. { - directive. }
  310. begin
  311. if (prevasmtoken=AS_ID) then
  312. begin
  313. c:=current_scanner^.asmgetchar;
  314. actasmtoken:=AS_DOT;
  315. exit;
  316. end;
  317. actasmpattern:=c;
  318. c:=current_scanner^.asmgetchar;
  319. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  320. begin
  321. actasmpattern:=actasmpattern + c;
  322. c:=current_scanner^.asmgetchar;
  323. end;
  324. if is_asmdirective(actasmpattern) then
  325. exit;
  326. { local label references and directives }
  327. { are case sensitive }
  328. actasmtoken:=AS_ID;
  329. exit;
  330. end;
  331. { identifier, register, prefix or directive }
  332. '_','A'..'Z','a'..'z':
  333. begin
  334. len:=0;
  335. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  336. begin
  337. inc(len);
  338. actasmpattern[len]:=c;
  339. c:=current_scanner^.asmgetchar;
  340. end;
  341. actasmpattern[0]:=chr(len);
  342. uppervar(actasmpattern);
  343. { Opcode, can only be when the previous was a prefix }
  344. If is_prefix(actopcode) and is_asmopcode(upper(actasmpattern)) then
  345. Begin
  346. uppervar(actasmpattern);
  347. exit;
  348. end;
  349. { check for end which is a reserved word unlike the opcodes }
  350. if actasmpattern = 'END' then
  351. Begin
  352. actasmtoken:=AS_END;
  353. exit;
  354. end;
  355. actasmtoken:=AS_ID;
  356. exit;
  357. end;
  358. '%' : { register or modulo }
  359. begin
  360. len:=1;
  361. actasmpattern[len]:='%';
  362. c:=current_scanner^.asmgetchar;
  363. while c in ['a'..'z','A'..'Z','0'..'9'] do
  364. Begin
  365. inc(len);
  366. actasmpattern[len]:=c;
  367. c:=current_scanner^.asmgetchar;
  368. end;
  369. actasmpattern[0]:=chr(len);
  370. uppervar(actasmpattern);
  371. if (actasmpattern = '%ST') and (c='(') then
  372. Begin
  373. actasmpattern:=actasmpattern+c;
  374. c:=current_scanner^.asmgetchar;
  375. if c in ['0'..'9'] then
  376. actasmpattern:=actasmpattern + c
  377. else
  378. Message(asmr_e_invalid_fpu_register);
  379. c:=current_scanner^.asmgetchar;
  380. if c <> ')' then
  381. Message(asmr_e_invalid_fpu_register)
  382. else
  383. Begin
  384. actasmpattern:=actasmpattern + c;
  385. c:=current_scanner^.asmgetchar; { let us point to next character. }
  386. end;
  387. end;
  388. if is_register(actasmpattern) then
  389. exit;
  390. Message(asmr_w_modulo_not_supported);
  391. end;
  392. '1'..'9': { integer number }
  393. begin
  394. len:=0;
  395. while c in ['0'..'9'] do
  396. Begin
  397. inc(len);
  398. actasmpattern[len]:=c;
  399. c:=current_scanner^.asmgetchar;
  400. end;
  401. actasmpattern[0]:=chr(len);
  402. actasmpattern:=tostr(ValDecimal(actasmpattern));
  403. actasmtoken:=AS_INTNUM;
  404. exit;
  405. end;
  406. '0' : { octal,hexa,real or binary number. }
  407. begin
  408. actasmpattern:=c;
  409. c:=current_scanner^.asmgetchar;
  410. case upcase(c) of
  411. 'B': { binary }
  412. Begin
  413. c:=current_scanner^.asmgetchar;
  414. while c in ['0','1'] do
  415. Begin
  416. actasmpattern:=actasmpattern + c;
  417. c:=current_scanner^.asmgetchar;
  418. end;
  419. actasmpattern:=tostr(ValBinary(actasmpattern));
  420. actasmtoken:=AS_INTNUM;
  421. exit;
  422. end;
  423. 'D': { real }
  424. Begin
  425. c:=current_scanner^.asmgetchar;
  426. { get ridd of the 0d }
  427. if (c in ['+','-']) then
  428. begin
  429. actasmpattern:=c;
  430. c:=current_scanner^.asmgetchar;
  431. end
  432. else
  433. actasmpattern:='';
  434. while c in ['0'..'9'] do
  435. Begin
  436. actasmpattern:=actasmpattern + c;
  437. c:=current_scanner^.asmgetchar;
  438. end;
  439. if c='.' then
  440. begin
  441. actasmpattern:=actasmpattern + c;
  442. c:=current_scanner^.asmgetchar;
  443. while c in ['0'..'9'] do
  444. Begin
  445. actasmpattern:=actasmpattern + c;
  446. c:=current_scanner^.asmgetchar;
  447. end;
  448. if upcase(c) = 'E' then
  449. begin
  450. actasmpattern:=actasmpattern + c;
  451. c:=current_scanner^.asmgetchar;
  452. if (c in ['+','-']) then
  453. begin
  454. actasmpattern:=actasmpattern + c;
  455. c:=current_scanner^.asmgetchar;
  456. end;
  457. while c in ['0'..'9'] do
  458. Begin
  459. actasmpattern:=actasmpattern + c;
  460. c:=current_scanner^.asmgetchar;
  461. end;
  462. end;
  463. actasmtoken:=AS_REALNUM;
  464. exit;
  465. end
  466. else
  467. begin
  468. Message1(asmr_e_invalid_float_const,actasmpattern+c);
  469. actasmtoken:=AS_NONE;
  470. end;
  471. end;
  472. 'X': { hexadecimal }
  473. Begin
  474. c:=current_scanner^.asmgetchar;
  475. while c in ['0'..'9','a'..'f','A'..'F'] do
  476. Begin
  477. actasmpattern:=actasmpattern + c;
  478. c:=current_scanner^.asmgetchar;
  479. end;
  480. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  481. actasmtoken:=AS_INTNUM;
  482. exit;
  483. end;
  484. '1'..'7': { octal }
  485. begin
  486. actasmpattern:=actasmpattern + c;
  487. while c in ['0'..'7'] do
  488. Begin
  489. actasmpattern:=actasmpattern + c;
  490. c:=current_scanner^.asmgetchar;
  491. end;
  492. actasmpattern:=tostr(ValOctal(actasmpattern));
  493. actasmtoken:=AS_INTNUM;
  494. exit;
  495. end;
  496. else { octal number zero value...}
  497. Begin
  498. actasmpattern:=tostr(ValOctal(actasmpattern));
  499. actasmtoken:=AS_INTNUM;
  500. exit;
  501. end;
  502. end; { end case }
  503. end;
  504. '&' :
  505. begin
  506. c:=current_scanner^.asmgetchar;
  507. actasmtoken:=AS_AND;
  508. end;
  509. '''' : { char }
  510. begin
  511. actasmpattern:='';
  512. repeat
  513. c:=current_scanner^.asmgetchar;
  514. case c of
  515. '\' :
  516. begin
  517. { copy also the next char so \" is parsed correctly }
  518. c:=current_scanner^.asmgetchar;
  519. actasmpattern:=actasmpattern+c;
  520. end;
  521. '''' :
  522. begin
  523. c:=current_scanner^.asmgetchar;
  524. break;
  525. end;
  526. newline:
  527. Message(scan_f_string_exceeds_line);
  528. else
  529. actasmpattern:=actasmpattern+c;
  530. end;
  531. until false;
  532. actasmpattern:=EscapeToPascal(actasmpattern);
  533. actasmtoken:=AS_STRING;
  534. exit;
  535. end;
  536. '"' : { string }
  537. begin
  538. actasmpattern:='';
  539. repeat
  540. c:=current_scanner^.asmgetchar;
  541. case c of
  542. '\' :
  543. begin
  544. { copy also the next char so \" is parsed correctly }
  545. c:=current_scanner^.asmgetchar;
  546. actasmpattern:=actasmpattern+c;
  547. end;
  548. '"' :
  549. begin
  550. c:=current_scanner^.asmgetchar;
  551. break;
  552. end;
  553. newline:
  554. Message(scan_f_string_exceeds_line);
  555. else
  556. actasmpattern:=actasmpattern+c;
  557. end;
  558. until false;
  559. actasmpattern:=EscapeToPascal(actasmpattern);
  560. actasmtoken:=AS_STRING;
  561. exit;
  562. end;
  563. '$' :
  564. begin
  565. actasmtoken:=AS_DOLLAR;
  566. c:=current_scanner^.asmgetchar;
  567. exit;
  568. end;
  569. ',' :
  570. begin
  571. actasmtoken:=AS_COMMA;
  572. c:=current_scanner^.asmgetchar;
  573. exit;
  574. end;
  575. '<' :
  576. begin
  577. actasmtoken:=AS_SHL;
  578. c:=current_scanner^.asmgetchar;
  579. if c = '<' then
  580. c:=current_scanner^.asmgetchar;
  581. exit;
  582. end;
  583. '>' :
  584. begin
  585. actasmtoken:=AS_SHL;
  586. c:=current_scanner^.asmgetchar;
  587. if c = '>' then
  588. c:=current_scanner^.asmgetchar;
  589. exit;
  590. end;
  591. '|' :
  592. begin
  593. actasmtoken:=AS_OR;
  594. c:=current_scanner^.asmgetchar;
  595. exit;
  596. end;
  597. '^' :
  598. begin
  599. actasmtoken:=AS_XOR;
  600. c:=current_scanner^.asmgetchar;
  601. exit;
  602. end;
  603. '!' :
  604. begin
  605. Message(asmr_e_nor_not_supported);
  606. c:=current_scanner^.asmgetchar;
  607. actasmtoken:=AS_NONE;
  608. exit;
  609. end;
  610. '(' :
  611. begin
  612. actasmtoken:=AS_LPAREN;
  613. c:=current_scanner^.asmgetchar;
  614. exit;
  615. end;
  616. ')' :
  617. begin
  618. actasmtoken:=AS_RPAREN;
  619. c:=current_scanner^.asmgetchar;
  620. exit;
  621. end;
  622. ':' :
  623. begin
  624. actasmtoken:=AS_COLON;
  625. c:=current_scanner^.asmgetchar;
  626. exit;
  627. end;
  628. '+' :
  629. begin
  630. actasmtoken:=AS_PLUS;
  631. c:=current_scanner^.asmgetchar;
  632. exit;
  633. end;
  634. '-' :
  635. begin
  636. actasmtoken:=AS_MINUS;
  637. c:=current_scanner^.asmgetchar;
  638. exit;
  639. end;
  640. '*' :
  641. begin
  642. actasmtoken:=AS_STAR;
  643. c:=current_scanner^.asmgetchar;
  644. exit;
  645. end;
  646. '/' :
  647. begin
  648. c:=current_scanner^.asmgetchar;
  649. actasmtoken:=AS_SLASH;
  650. exit;
  651. end;
  652. '{',#13,newline,';' :
  653. begin
  654. { the comment is read by asmgetchar }
  655. c:=current_scanner^.asmgetchar;
  656. firsttoken:=TRUE;
  657. actasmtoken:=AS_SEPARATOR;
  658. exit;
  659. end;
  660. else
  661. Begin
  662. Message(scan_f_illegal_char);
  663. end;
  664. end;
  665. end;
  666. end;
  667. function consume(t : tasmtoken):boolean;
  668. begin
  669. Consume:=true;
  670. if t<>actasmtoken then
  671. begin
  672. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  673. Consume:=false;
  674. end;
  675. repeat
  676. gettoken;
  677. until actasmtoken<>AS_NONE;
  678. end;
  679. procedure RecoverConsume(allowcomma:boolean);
  680. begin
  681. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  682. begin
  683. if allowcomma and (actasmtoken=AS_COMMA) then
  684. break;
  685. Consume(actasmtoken);
  686. end;
  687. end;
  688. {*****************************************************************************
  689. Parsing Helpers
  690. *****************************************************************************}
  691. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  692. { Description: This routine builds up a record offset after a AS_DOT }
  693. { token is encountered. }
  694. { On entry actasmtoken should be equal to AS_DOT }
  695. var
  696. s : string;
  697. Begin
  698. offset:=0;
  699. size:=0;
  700. s:=expr;
  701. while (actasmtoken=AS_DOT) do
  702. begin
  703. Consume(AS_DOT);
  704. if actasmtoken=AS_ID then
  705. s:=s+'.'+actasmpattern;
  706. if not Consume(AS_ID) then
  707. begin
  708. RecoverConsume(true);
  709. break;
  710. end;
  711. end;
  712. if not GetRecordOffsetSize(s,offset,size) then
  713. Message(asmr_e_building_record_offset);
  714. end;
  715. Procedure BuildConstSymbolExpression(allowref,betweenbracket,needofs:boolean;var value:longint;var asmsym:string);
  716. var
  717. hs,tempstr,expr : string;
  718. parenlevel,l,k : longint;
  719. errorflag : boolean;
  720. prevtok : tasmtoken;
  721. sym : psym;
  722. hl : PAsmLabel;
  723. Begin
  724. asmsym:='';
  725. value:=0;
  726. errorflag:=FALSE;
  727. tempstr:='';
  728. expr:='';
  729. parenlevel:=0;
  730. Repeat
  731. Case actasmtoken of
  732. AS_LPAREN:
  733. Begin
  734. { Exit if ref? }
  735. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  736. break;
  737. Consume(AS_LPAREN);
  738. expr:=expr + '(';
  739. inc(parenlevel);
  740. end;
  741. AS_RPAREN:
  742. Begin
  743. { end of ref ? }
  744. if (parenlevel=0) and betweenbracket then
  745. break;
  746. Consume(AS_RPAREN);
  747. expr:=expr + ')';
  748. dec(parenlevel);
  749. end;
  750. AS_SHL:
  751. Begin
  752. Consume(AS_SHL);
  753. expr:=expr + '<';
  754. end;
  755. AS_SHR:
  756. Begin
  757. Consume(AS_SHR);
  758. expr:=expr + '>';
  759. end;
  760. AS_SLASH:
  761. Begin
  762. Consume(AS_SLASH);
  763. expr:=expr + '/';
  764. end;
  765. AS_MOD:
  766. Begin
  767. Consume(AS_MOD);
  768. expr:=expr + '%';
  769. end;
  770. AS_STAR:
  771. Begin
  772. Consume(AS_STAR);
  773. expr:=expr + '*';
  774. end;
  775. AS_PLUS:
  776. Begin
  777. Consume(AS_PLUS);
  778. expr:=expr + '+';
  779. end;
  780. AS_MINUS:
  781. Begin
  782. Consume(AS_MINUS);
  783. expr:=expr + '-';
  784. end;
  785. AS_AND:
  786. Begin
  787. Consume(AS_AND);
  788. expr:=expr + '&';
  789. end;
  790. AS_NOT:
  791. Begin
  792. Consume(AS_NOT);
  793. expr:=expr + '~';
  794. end;
  795. AS_XOR:
  796. Begin
  797. Consume(AS_XOR);
  798. expr:=expr + '^';
  799. end;
  800. AS_OR:
  801. Begin
  802. Consume(AS_OR);
  803. expr:=expr + '|';
  804. end;
  805. AS_INTNUM:
  806. Begin
  807. expr:=expr + actasmpattern;
  808. Consume(AS_INTNUM);
  809. end;
  810. AS_DOLLAR:
  811. begin
  812. Consume(AS_DOLLAR);
  813. if actasmtoken<>AS_ID then
  814. Message(asmr_e_dollar_without_identifier);
  815. end;
  816. AS_STRING:
  817. Begin
  818. l:=0;
  819. case Length(actasmpattern) of
  820. 1 :
  821. l:=ord(actasmpattern[1]);
  822. 2 :
  823. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  824. 3 :
  825. l:=ord(actasmpattern[3]) +
  826. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  827. 4 :
  828. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  829. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  830. else
  831. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  832. end;
  833. str(l, tempstr);
  834. expr:=expr + tempstr;
  835. Consume(AS_STRING);
  836. end;
  837. AS_ID:
  838. Begin
  839. tempstr:=actasmpattern;
  840. prevtok:=prevasmtoken;
  841. consume(AS_ID);
  842. if actasmtoken=AS_DOT then
  843. begin
  844. BuildRecordOffsetSize(tempstr,l,k);
  845. str(l, tempstr);
  846. expr:=expr + tempstr;
  847. end
  848. else
  849. if SearchIConstant(tempstr,l) then
  850. begin
  851. str(l, tempstr);
  852. expr:=expr + tempstr;
  853. end
  854. else
  855. begin
  856. hs:='';
  857. if is_locallabel(tempstr) then
  858. begin
  859. CreateLocalLabel(tempstr,hl,false);
  860. hs:=hl^.name
  861. end
  862. else
  863. if SearchLabel(tempstr,hl,false) then
  864. hs:=hl^.name
  865. else
  866. begin
  867. getsym(tempstr,false);
  868. sym:=srsym;
  869. if assigned(sym) then
  870. begin
  871. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  872. Message(asmr_e_no_local_or_para_allowed);
  873. case srsym^.typ of
  874. varsym :
  875. hs:=pvarsym(srsym)^.mangledname;
  876. typedconstsym :
  877. hs:=ptypedconstsym(srsym)^.mangledname;
  878. procsym :
  879. hs:=pprocsym(srsym)^.mangledname;
  880. else
  881. Message(asmr_e_wrong_sym_type);
  882. end;
  883. end
  884. else
  885. Message1(sym_e_unknown_id,tempstr);
  886. end;
  887. { symbol found? }
  888. if hs<>'' then
  889. begin
  890. if needofs and (prevtok<>AS_DOLLAR) then
  891. Message(asmr_e_need_offset);
  892. if asmsym='' then
  893. asmsym:=hs
  894. else
  895. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  896. if (expr='') or (expr[length(expr)]='+') then
  897. begin
  898. delete(expr,length(expr),1);
  899. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  900. Message(asmr_e_only_add_relocatable_symbol);
  901. end
  902. else
  903. Message(asmr_e_only_add_relocatable_symbol);
  904. end
  905. else
  906. begin
  907. { Error recovery }
  908. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  909. delete(expr,length(expr),1);
  910. end;
  911. end;
  912. end;
  913. AS_END,
  914. AS_SEPARATOR,
  915. AS_COMMA:
  916. Begin
  917. break;
  918. end;
  919. else
  920. Begin
  921. { write error only once. }
  922. if not errorflag then
  923. Message(asmr_e_invalid_constant_expression);
  924. { consume tokens until we find COMMA or SEPARATOR }
  925. Consume(actasmtoken);
  926. errorflag:=TRUE;
  927. end;
  928. end;
  929. Until false;
  930. { calculate expression }
  931. if not ErrorFlag then
  932. value:=CalculateExpression(expr)
  933. else
  934. value:=0;
  935. end;
  936. Function BuildConstExpression(allowref,betweenbracket:boolean): longint;
  937. var
  938. l : longint;
  939. hs : string;
  940. begin
  941. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs);
  942. if hs<>'' then
  943. Message(asmr_e_relocatable_symbol_not_allowed);
  944. BuildConstExpression:=l;
  945. end;
  946. {****************************************************************************
  947. T386ATTOperand
  948. ****************************************************************************}
  949. type
  950. P386ATTOperand=^T386ATTOperand;
  951. T386ATTOperand=object(T386Operand)
  952. Procedure BuildOperand;virtual;
  953. private
  954. Procedure BuildReference;
  955. Procedure BuildConstant;
  956. end;
  957. Procedure T386ATTOperand.BuildReference;
  958. procedure Consume_RParen;
  959. begin
  960. if actasmtoken <> AS_RPAREN then
  961. Begin
  962. Message(asmr_e_invalid_reference_syntax);
  963. RecoverConsume(true);
  964. end
  965. else
  966. begin
  967. Consume(AS_RPAREN);
  968. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  969. Begin
  970. Message(asmr_e_invalid_reference_syntax);
  971. RecoverConsume(true);
  972. end;
  973. end;
  974. end;
  975. procedure Consume_Scale;
  976. var
  977. l : longint;
  978. begin
  979. { we have to process the scaling }
  980. l:=BuildConstExpression(false,true);
  981. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  982. opr.ref.scalefactor:=l
  983. else
  984. Begin
  985. Message(asmr_e_wrong_scale_factor);
  986. opr.ref.scalefactor:=0;
  987. end;
  988. end;
  989. Begin
  990. Consume(AS_LPAREN);
  991. Case actasmtoken of
  992. AS_INTNUM,
  993. AS_MINUS,
  994. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  995. Begin
  996. { offset(offset) is invalid }
  997. If opr.Ref.Offset <> 0 Then
  998. Begin
  999. Message(asmr_e_invalid_reference_syntax);
  1000. RecoverConsume(true);
  1001. End
  1002. Else
  1003. Begin
  1004. opr.Ref.Offset:=BuildConstExpression(false,true);
  1005. Consume_RParen;
  1006. end;
  1007. exit;
  1008. End;
  1009. AS_REGISTER: { (reg ... }
  1010. Begin
  1011. { Check if there is already a base (mostly ebp,esp) than this is
  1012. not allowed,becuase it will give crashing code }
  1013. if opr.ref.base<>R_NO then
  1014. Message(asmr_e_cannot_index_relative_var);
  1015. opr.ref.base:=actasmregister;
  1016. Consume(AS_REGISTER);
  1017. { can either be a register or a right parenthesis }
  1018. { (reg) }
  1019. if actasmtoken=AS_RPAREN then
  1020. Begin
  1021. Consume_RParen;
  1022. exit;
  1023. end;
  1024. { (reg,reg .. }
  1025. Consume(AS_COMMA);
  1026. if actasmtoken=AS_REGISTER then
  1027. Begin
  1028. opr.ref.index:=actasmregister;
  1029. Consume(AS_REGISTER);
  1030. { check for scaling ... }
  1031. case actasmtoken of
  1032. AS_RPAREN:
  1033. Begin
  1034. Consume_RParen;
  1035. exit;
  1036. end;
  1037. AS_COMMA:
  1038. Begin
  1039. Consume(AS_COMMA);
  1040. Consume_Scale;
  1041. Consume_RParen;
  1042. end;
  1043. else
  1044. Begin
  1045. Message(asmr_e_invalid_reference_syntax);
  1046. RecoverConsume(false);
  1047. end;
  1048. end; { end case }
  1049. end
  1050. else
  1051. Begin
  1052. Message(asmr_e_invalid_reference_syntax);
  1053. RecoverConsume(false);
  1054. end;
  1055. end; {end case }
  1056. AS_COMMA: { (, ... can either be scaling, or index }
  1057. Begin
  1058. Consume(AS_COMMA);
  1059. { Index }
  1060. if (actasmtoken=AS_REGISTER) then
  1061. Begin
  1062. opr.ref.index:=actasmregister;
  1063. Consume(AS_REGISTER);
  1064. { check for scaling ... }
  1065. case actasmtoken of
  1066. AS_RPAREN:
  1067. Begin
  1068. Consume_RParen;
  1069. exit;
  1070. end;
  1071. AS_COMMA:
  1072. Begin
  1073. Consume(AS_COMMA);
  1074. Consume_Scale;
  1075. Consume_RParen;
  1076. end;
  1077. else
  1078. Begin
  1079. Message(asmr_e_invalid_reference_syntax);
  1080. RecoverConsume(false);
  1081. end;
  1082. end; {end case }
  1083. end
  1084. { Scaling }
  1085. else
  1086. Begin
  1087. Consume_Scale;
  1088. Consume_RParen;
  1089. exit;
  1090. end;
  1091. end;
  1092. else
  1093. Begin
  1094. Message(asmr_e_invalid_reference_syntax);
  1095. RecoverConsume(false);
  1096. end;
  1097. end;
  1098. end;
  1099. Procedure T386ATTOperand.BuildConstant;
  1100. var
  1101. l : longint;
  1102. tempstr : string;
  1103. begin
  1104. BuildConstSymbolExpression(false,false,true,l,tempstr);
  1105. if tempstr<>'' then
  1106. begin
  1107. opr.typ:=OPR_SYMBOL;
  1108. opr.symofs:=l;
  1109. opr.symbol:=newasmsymbol(tempstr);
  1110. end
  1111. else
  1112. begin
  1113. opr.typ:=OPR_CONSTANT;
  1114. opr.val:=l;
  1115. end;
  1116. end;
  1117. Procedure T386ATTOperand.BuildOperand;
  1118. var
  1119. expr : string;
  1120. procedure AddLabelOperand(hl:pasmlabel);
  1121. begin
  1122. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  1123. is_calljmp(actopcode) then
  1124. begin
  1125. opr.typ:=OPR_SYMBOL;
  1126. opr.symbol:=hl;
  1127. end
  1128. else
  1129. begin
  1130. InitRef;
  1131. opr.ref.symbol:=hl;
  1132. end;
  1133. end;
  1134. procedure MaybeRecordOffset;
  1135. var
  1136. hasdot : boolean;
  1137. l,
  1138. toffset,
  1139. tsize : longint;
  1140. begin
  1141. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  1142. exit;
  1143. l:=0;
  1144. hasdot:=(actasmtoken=AS_DOT);
  1145. if hasdot then
  1146. begin
  1147. if expr<>'' then
  1148. begin
  1149. BuildRecordOffsetSize(expr,toffset,tsize);
  1150. inc(l,toffset);
  1151. SetSize(tsize);
  1152. end;
  1153. end;
  1154. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1155. inc(l,BuildConstExpression(true,false));
  1156. if opr.typ=OPR_REFERENCE then
  1157. begin
  1158. if hasdot and (opr.ref.options=ref_parafixup) then
  1159. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1160. inc(opr.ref.offset,l)
  1161. end
  1162. else
  1163. inc(opr.val,l);
  1164. end;
  1165. function MaybeBuildReference:boolean;
  1166. { Try to create a reference, if not a reference is found then false
  1167. is returned }
  1168. begin
  1169. MaybeBuildReference:=true;
  1170. case actasmtoken of
  1171. AS_INTNUM,
  1172. AS_MINUS,
  1173. AS_PLUS:
  1174. Begin
  1175. opr.ref.offset:=BuildConstExpression(True,False);
  1176. if actasmtoken<>AS_LPAREN then
  1177. Message(asmr_e_invalid_reference_syntax)
  1178. else
  1179. BuildReference;
  1180. end;
  1181. AS_LPAREN:
  1182. BuildReference;
  1183. AS_ID: { only a variable is allowed ... }
  1184. Begin
  1185. if not SetupVar(actasmpattern,false) then
  1186. Message(asmr_e_invalid_reference_syntax);
  1187. Consume(AS_ID);
  1188. case actasmtoken of
  1189. AS_END,
  1190. AS_SEPARATOR,
  1191. AS_COMMA: ;
  1192. AS_LPAREN: BuildReference;
  1193. else
  1194. Begin
  1195. Message(asmr_e_invalid_reference_syntax);
  1196. Consume(actasmtoken);
  1197. end;
  1198. end; {end case }
  1199. end;
  1200. else
  1201. MaybeBuildReference:=false;
  1202. end; { end case }
  1203. end;
  1204. var
  1205. tempreg : tregister;
  1206. hl : PAsmLabel;
  1207. l : longint;
  1208. Begin
  1209. expr:='';
  1210. case actasmtoken of
  1211. AS_LPAREN: { Memory reference or constant expression }
  1212. Begin
  1213. InitRef;
  1214. BuildReference;
  1215. end;
  1216. AS_DOLLAR: { Constant expression }
  1217. Begin
  1218. Consume(AS_DOLLAR);
  1219. BuildConstant;
  1220. end;
  1221. AS_INTNUM,
  1222. AS_MINUS,
  1223. AS_PLUS:
  1224. Begin
  1225. { Constant memory offset }
  1226. { This must absolutely be followed by ( }
  1227. InitRef;
  1228. opr.ref.offset:=BuildConstExpression(True,False);
  1229. if actasmtoken<>AS_LPAREN then
  1230. Message(asmr_e_invalid_reference_syntax)
  1231. else
  1232. BuildReference;
  1233. end;
  1234. AS_STAR: { Call from memory address }
  1235. Begin
  1236. Consume(AS_STAR);
  1237. if actasmtoken=AS_REGISTER then
  1238. begin
  1239. opr.typ:=OPR_REGISTER;
  1240. opr.reg:=actasmregister;
  1241. size:=reg_2_opsize[actasmregister];
  1242. Consume(AS_REGISTER);
  1243. end
  1244. else
  1245. begin
  1246. InitRef;
  1247. if not MaybeBuildReference then
  1248. Message(asmr_e_syn_operand);
  1249. end;
  1250. { this is only allowed for call's and jmp's }
  1251. if not is_calljmp(actopcode) then
  1252. Message(asmr_e_syn_operand);
  1253. end;
  1254. AS_ID: { A constant expression, or a Variable ref. }
  1255. Begin
  1256. { Local Label ? }
  1257. if is_locallabel(actasmpattern) then
  1258. begin
  1259. CreateLocalLabel(actasmpattern,hl,false);
  1260. Consume(AS_ID);
  1261. AddLabelOperand(hl);
  1262. end
  1263. else
  1264. { Check for label }
  1265. if SearchLabel(actasmpattern,hl,false) then
  1266. begin
  1267. Consume(AS_ID);
  1268. AddLabelOperand(hl);
  1269. end
  1270. else
  1271. { probably a variable or normal expression }
  1272. { or a procedure (such as in CALL ID) }
  1273. Begin
  1274. { is it a constant ? }
  1275. if SearchIConstant(actasmpattern,l) then
  1276. Begin
  1277. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1278. Message(asmr_e_invalid_operand_type);
  1279. BuildConstant;
  1280. end
  1281. else
  1282. begin
  1283. InitRef;
  1284. if SetupVar(actasmpattern,false) then
  1285. begin
  1286. expr:=actasmpattern;
  1287. Consume(AS_ID);
  1288. MaybeRecordOffset;
  1289. { add a constant expression? }
  1290. if (actasmtoken=AS_PLUS) then
  1291. begin
  1292. l:=BuildConstExpression(true,false);
  1293. if opr.typ=OPR_CONSTANT then
  1294. inc(opr.val,l)
  1295. else
  1296. inc(opr.ref.offset,l);
  1297. end
  1298. end
  1299. else
  1300. Begin
  1301. { look for special symbols ... }
  1302. if actasmpattern = '__RESULT' then
  1303. SetUpResult
  1304. else
  1305. if actasmpattern = '__SELF' then
  1306. SetupSelf
  1307. else
  1308. if actasmpattern = '__OLDEBP' then
  1309. SetupOldEBP
  1310. else
  1311. { check for direct symbolic names }
  1312. { only if compiling the system unit }
  1313. if (cs_compilesystem in aktmoduleswitches) then
  1314. begin
  1315. if not SetupDirectVar(actasmpattern) then
  1316. Begin
  1317. { not found, finally ... add it anyways ... }
  1318. Message1(asmr_w_id_supposed_external,actasmpattern);
  1319. opr.ref.symbol:=newasmsymbol(actasmpattern);
  1320. end;
  1321. end
  1322. else
  1323. Message1(sym_e_unknown_id,actasmpattern);
  1324. Consume(AS_ID);
  1325. end;
  1326. end;
  1327. end;
  1328. { Do we have a indexing reference, then parse it also }
  1329. if actasmtoken=AS_LPAREN then
  1330. begin
  1331. if (opr.typ=OPR_CONSTANT) then
  1332. begin
  1333. l:=opr.val;
  1334. opr.typ:=OPR_REFERENCE;
  1335. reset_reference(opr.Ref);
  1336. opr.Ref.Offset:=l;
  1337. end;
  1338. BuildReference;
  1339. end;
  1340. end;
  1341. AS_REGISTER: { Register, a variable reference or a constant reference }
  1342. Begin
  1343. { save the type of register used. }
  1344. tempreg:=actasmregister;
  1345. Consume(AS_REGISTER);
  1346. if actasmtoken = AS_COLON then
  1347. Begin
  1348. Consume(AS_COLON);
  1349. InitRef;
  1350. opr.ref.segment:=tempreg;
  1351. { This must absolutely be followed by a reference }
  1352. if not MaybeBuildReference then
  1353. Begin
  1354. Message(asmr_e_invalid_seg_override);
  1355. Consume(actasmtoken);
  1356. end;
  1357. end
  1358. { Simple register }
  1359. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1360. Begin
  1361. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1362. Message(asmr_e_invalid_operand_type);
  1363. opr.typ:=OPR_REGISTER;
  1364. opr.reg:=tempreg;
  1365. size:=reg_2_opsize[tempreg];
  1366. end
  1367. else
  1368. Message(asmr_e_syn_operand);
  1369. end;
  1370. AS_END,
  1371. AS_SEPARATOR,
  1372. AS_COMMA: ;
  1373. else
  1374. Begin
  1375. Message(asmr_e_syn_operand);
  1376. Consume(actasmtoken);
  1377. end;
  1378. end; { end case }
  1379. end;
  1380. {*****************************************************************************
  1381. T386ATTInstruction
  1382. *****************************************************************************}
  1383. type
  1384. P386AttInstruction=^T386AttInstruction;
  1385. T386AttInstruction=object(T386Instruction)
  1386. procedure InitOperands;virtual;
  1387. procedure BuildOpcode;virtual;
  1388. end;
  1389. procedure T386AttInstruction.InitOperands;
  1390. var
  1391. i : longint;
  1392. begin
  1393. for i:=1to 3 do
  1394. Operands[i]:=new(P386AttOperand,Init);
  1395. end;
  1396. Procedure T386AttInstruction.BuildOpCode;
  1397. var
  1398. operandnum : longint;
  1399. PrefixOp,OverrideOp: tasmop;
  1400. Begin
  1401. PrefixOp:=A_None;
  1402. OverrideOp:=A_None;
  1403. { prefix seg opcode / prefix opcode }
  1404. repeat
  1405. if is_prefix(actopcode) then
  1406. begin
  1407. PrefixOp:=ActOpcode;
  1408. opcode:=ActOpcode;
  1409. condition:=ActCondition;
  1410. opsize:=ActOpsize;
  1411. ConcatInstruction(curlist);
  1412. Consume(AS_OPCODE);
  1413. end
  1414. else
  1415. if is_override(actopcode) then
  1416. begin
  1417. OverrideOp:=ActOpcode;
  1418. opcode:=ActOpcode;
  1419. condition:=ActCondition;
  1420. opsize:=ActOpsize;
  1421. ConcatInstruction(curlist);
  1422. Consume(AS_OPCODE);
  1423. end
  1424. else
  1425. break;
  1426. { allow for newline as in gas styled syntax }
  1427. while actasmtoken=AS_SEPARATOR do
  1428. Consume(AS_SEPARATOR);
  1429. until (actasmtoken<>AS_OPCODE);
  1430. { opcode }
  1431. if (actasmtoken <> AS_OPCODE) then
  1432. Begin
  1433. Message(asmr_e_invalid_or_missing_opcode);
  1434. RecoverConsume(true);
  1435. exit;
  1436. end;
  1437. { Fill the instr object with the current state }
  1438. Opcode:=ActOpcode;
  1439. condition:=ActCondition;
  1440. opsize:=ActOpsize;
  1441. { Valid combination of prefix/override and instruction ? }
  1442. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1443. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1444. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1445. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1446. { We are reading operands, so opcode will be an AS_ID }
  1447. operandnum:=1;
  1448. Consume(AS_OPCODE);
  1449. { Zero operand opcode ? }
  1450. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1451. begin
  1452. operandnum:=0;
  1453. exit;
  1454. end;
  1455. { Read the operands }
  1456. repeat
  1457. case actasmtoken of
  1458. AS_COMMA: { Operand delimiter }
  1459. Begin
  1460. if operandnum > MaxOperands then
  1461. Message(asmr_e_too_many_operands)
  1462. else
  1463. Inc(operandnum);
  1464. Consume(AS_COMMA);
  1465. end;
  1466. AS_SEPARATOR,
  1467. AS_END : { End of asm operands for this opcode }
  1468. begin
  1469. break;
  1470. end;
  1471. else
  1472. Operands[operandnum]^.BuildOperand;
  1473. end; { end case }
  1474. until false;
  1475. Ops:=operandnum;
  1476. end;
  1477. Procedure BuildConstant(maxvalue: longint);
  1478. var
  1479. asmsym,
  1480. expr: string;
  1481. value : longint;
  1482. Begin
  1483. Repeat
  1484. Case actasmtoken of
  1485. AS_STRING:
  1486. Begin
  1487. expr:=actasmpattern;
  1488. if length(expr) > 1 then
  1489. Message(asmr_e_string_not_allowed_as_const);
  1490. Consume(AS_STRING);
  1491. Case actasmtoken of
  1492. AS_COMMA: Consume(AS_COMMA);
  1493. AS_END,
  1494. AS_SEPARATOR: ;
  1495. else
  1496. Message(asmr_e_invalid_string_expression);
  1497. end; { end case }
  1498. ConcatString(curlist,expr);
  1499. end;
  1500. AS_INTNUM,
  1501. AS_PLUS,
  1502. AS_MINUS,
  1503. AS_LPAREN,
  1504. AS_NOT,
  1505. AS_ID :
  1506. Begin
  1507. BuildConstSymbolExpression(false,false,false,value,asmsym);
  1508. if asmsym<>'' then
  1509. begin
  1510. if maxvalue<>$ffffffff then
  1511. Message(asmr_w_32bit_const_for_address);
  1512. ConcatConstSymbol(curlist,asmsym,value)
  1513. end
  1514. else
  1515. ConcatConstant(curlist,value,maxvalue);
  1516. end;
  1517. AS_COMMA:
  1518. Consume(AS_COMMA);
  1519. AS_END,
  1520. AS_SEPARATOR:
  1521. break;
  1522. else
  1523. begin
  1524. Message(asmr_e_syn_constant);
  1525. RecoverConsume(false);
  1526. end
  1527. end; { end case }
  1528. Until false;
  1529. end;
  1530. Procedure BuildRealConstant(typ : tfloattype);
  1531. var
  1532. expr : string;
  1533. r : bestreal;
  1534. code : integer;
  1535. negativ : boolean;
  1536. errorflag: boolean;
  1537. Begin
  1538. errorflag:=FALSE;
  1539. Repeat
  1540. negativ:=false;
  1541. expr:='';
  1542. if actasmtoken=AS_PLUS then
  1543. Consume(AS_PLUS)
  1544. else
  1545. if actasmtoken=AS_MINUS then
  1546. begin
  1547. negativ:=true;
  1548. consume(AS_MINUS);
  1549. end;
  1550. Case actasmtoken of
  1551. AS_INTNUM:
  1552. Begin
  1553. expr:=actasmpattern;
  1554. Consume(AS_INTNUM);
  1555. if negativ then
  1556. expr:='-'+expr;
  1557. val(expr,r,code);
  1558. if code<>0 then
  1559. Begin
  1560. r:=0;
  1561. Message(asmr_e_invalid_float_expr);
  1562. End;
  1563. ConcatRealConstant(curlist,r,typ);
  1564. end;
  1565. AS_REALNUM:
  1566. Begin
  1567. expr:=actasmpattern;
  1568. Consume(AS_REALNUM);
  1569. { in ATT syntax you have 0d in front of the real }
  1570. { should this be forced ? yes i think so, as to }
  1571. { conform to gas as much as possible. }
  1572. if (expr[1]='0') and (upper(expr[2])='D') then
  1573. Delete(expr,1,2);
  1574. if negativ then
  1575. expr:='-'+expr;
  1576. val(expr,r,code);
  1577. if code<>0 then
  1578. Begin
  1579. r:=0;
  1580. Message(asmr_e_invalid_float_expr);
  1581. End;
  1582. ConcatRealConstant(curlist,r,typ);
  1583. end;
  1584. AS_COMMA:
  1585. begin
  1586. Consume(AS_COMMA);
  1587. end;
  1588. AS_END,
  1589. AS_SEPARATOR:
  1590. begin
  1591. break;
  1592. end;
  1593. else
  1594. Begin
  1595. Consume(actasmtoken);
  1596. if not errorflag then
  1597. Message(asmr_e_invalid_float_expr);
  1598. errorflag:=TRUE;
  1599. end;
  1600. end;
  1601. Until false;
  1602. end;
  1603. Procedure BuildStringConstant(asciiz: boolean);
  1604. var
  1605. expr: string;
  1606. errorflag : boolean;
  1607. Begin
  1608. errorflag:=FALSE;
  1609. Repeat
  1610. Case actasmtoken of
  1611. AS_STRING:
  1612. Begin
  1613. expr:=actasmpattern;
  1614. if asciiz then
  1615. expr:=expr+#0;
  1616. ConcatPasString(curlist,expr);
  1617. Consume(AS_STRING);
  1618. end;
  1619. AS_COMMA:
  1620. begin
  1621. Consume(AS_COMMA);
  1622. end;
  1623. AS_END,
  1624. AS_SEPARATOR:
  1625. begin
  1626. break;
  1627. end;
  1628. else
  1629. Begin
  1630. Consume(actasmtoken);
  1631. if not errorflag then
  1632. Message(asmr_e_invalid_string_expression);
  1633. errorflag:=TRUE;
  1634. end;
  1635. end;
  1636. Until false;
  1637. end;
  1638. Function Assemble: Ptree;
  1639. Var
  1640. hl : PAsmLabel;
  1641. commname : string;
  1642. lastsec : tsection;
  1643. l1,l2 : longint;
  1644. instr : T386ATTInstruction;
  1645. Begin
  1646. Message1(asmr_d_start_reading,'AT&T');
  1647. firsttoken:=TRUE;
  1648. if assigned(procinfo^.returntype.def) and
  1649. (is_fpu(procinfo^.returntype.def) or
  1650. ret_in_acc(procinfo^.returntype.def)) then
  1651. procinfo^.funcret_state:=vs_assigned;
  1652. { sets up all opcode and register tables in uppercase }
  1653. if not _asmsorted then
  1654. Begin
  1655. SetupTables;
  1656. _asmsorted:=TRUE;
  1657. end;
  1658. curlist:=new(paasmoutput,init);
  1659. lastsec:=sec_code;
  1660. { setup label linked list }
  1661. new(LocalLabelList,Init);
  1662. { start tokenizer }
  1663. c:=current_scanner^.asmgetchar;
  1664. gettoken;
  1665. { main loop }
  1666. repeat
  1667. case actasmtoken of
  1668. AS_LLABEL:
  1669. Begin
  1670. if CreateLocalLabel(actasmpattern,hl,true) then
  1671. ConcatLabel(curlist,hl);
  1672. Consume(AS_LLABEL);
  1673. end;
  1674. AS_LABEL:
  1675. Begin
  1676. if SearchLabel(upper(actasmpattern),hl,true) then
  1677. ConcatLabel(curlist,hl)
  1678. else
  1679. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1680. Consume(AS_LABEL);
  1681. end;
  1682. AS_DW:
  1683. Begin
  1684. Consume(AS_DW);
  1685. BuildConstant($ffff);
  1686. end;
  1687. AS_DATA:
  1688. Begin
  1689. curlist^.Concat(new(pai_section,init(sec_data)));
  1690. lastsec:=sec_data;
  1691. Consume(AS_DATA);
  1692. end;
  1693. AS_TEXT:
  1694. Begin
  1695. curlist^.Concat(new(pai_section,init(sec_code)));
  1696. lastsec:=sec_code;
  1697. Consume(AS_TEXT);
  1698. end;
  1699. AS_DB:
  1700. Begin
  1701. Consume(AS_DB);
  1702. BuildConstant($ff);
  1703. end;
  1704. AS_DD:
  1705. Begin
  1706. Consume(AS_DD);
  1707. BuildConstant($ffffffff);
  1708. end;
  1709. AS_DQ:
  1710. Begin
  1711. Consume(AS_DQ);
  1712. BuildRealConstant(s64comp);
  1713. end;
  1714. AS_SINGLE:
  1715. Begin
  1716. Consume(AS_SINGLE);
  1717. BuildRealConstant(s32real);
  1718. end;
  1719. AS_DOUBLE:
  1720. Begin
  1721. Consume(AS_DOUBLE);
  1722. BuildRealConstant(s64real);
  1723. end;
  1724. AS_EXTENDED:
  1725. Begin
  1726. Consume(AS_EXTENDED);
  1727. BuildRealConstant(s80real);
  1728. end;
  1729. AS_GLOBAL:
  1730. Begin
  1731. Consume(AS_GLOBAL);
  1732. if actasmtoken=AS_ID then
  1733. ConcatPublic(curlist,actasmpattern);
  1734. Consume(AS_ID);
  1735. if actasmtoken<>AS_SEPARATOR then
  1736. Consume(AS_SEPARATOR);
  1737. end;
  1738. AS_ALIGN:
  1739. Begin
  1740. Consume(AS_ALIGN);
  1741. l1:=BuildConstExpression(false,false);
  1742. if (target_info.target in [target_i386_GO32V1,target_i386_GO32V2]) then
  1743. begin
  1744. l2:=1;
  1745. if (l1>=0) and (l1<=16) then
  1746. while (l1>0) do
  1747. begin
  1748. l2:=2*l2;
  1749. dec(l1);
  1750. end;
  1751. l1:=l2;
  1752. end;
  1753. ConcatAlign(curlist,l1);
  1754. Message(asmr_n_align_is_target_specific);
  1755. if actasmtoken<>AS_SEPARATOR then
  1756. Consume(AS_SEPARATOR);
  1757. end;
  1758. AS_BALIGN:
  1759. Begin
  1760. Consume(AS_BALIGN);
  1761. ConcatAlign(curlist,BuildConstExpression(false,false));
  1762. if actasmtoken<>AS_SEPARATOR then
  1763. Consume(AS_SEPARATOR);
  1764. end;
  1765. AS_P2ALIGN:
  1766. Begin
  1767. Consume(AS_P2ALIGN);
  1768. l1:=BuildConstExpression(false,false);
  1769. l2:=1;
  1770. if (l1>=0) and (l1<=16) then
  1771. while (l1>0) do
  1772. begin
  1773. l2:=2*l2;
  1774. dec(l1);
  1775. end;
  1776. l1:=l2;
  1777. ConcatAlign(curlist,l1);
  1778. if actasmtoken<>AS_SEPARATOR then
  1779. Consume(AS_SEPARATOR);
  1780. end;
  1781. AS_ASCIIZ:
  1782. Begin
  1783. Consume(AS_ASCIIZ);
  1784. BuildStringConstant(TRUE);
  1785. end;
  1786. AS_ASCII:
  1787. Begin
  1788. Consume(AS_ASCII);
  1789. BuildStringConstant(FALSE);
  1790. end;
  1791. AS_LCOMM:
  1792. Begin
  1793. Consume(AS_LCOMM);
  1794. commname:=actasmpattern;
  1795. Consume(AS_ID);
  1796. Consume(AS_COMMA);
  1797. ConcatLocalBss(commname,BuildConstExpression(false,false));
  1798. if actasmtoken<>AS_SEPARATOR then
  1799. Consume(AS_SEPARATOR);
  1800. end;
  1801. AS_COMM:
  1802. Begin
  1803. Consume(AS_COMM);
  1804. commname:=actasmpattern;
  1805. Consume(AS_ID);
  1806. Consume(AS_COMMA);
  1807. ConcatGlobalBss(commname,BuildConstExpression(false,false));
  1808. if actasmtoken<>AS_SEPARATOR then
  1809. Consume(AS_SEPARATOR);
  1810. end;
  1811. AS_OPCODE:
  1812. Begin
  1813. instr.init;
  1814. instr.BuildOpcode;
  1815. instr.AddReferenceSizes;
  1816. instr.SetInstructionOpsize;
  1817. instr.CheckOperandSizes;
  1818. instr.ConcatInstruction(curlist);
  1819. instr.done;
  1820. end;
  1821. AS_SEPARATOR:
  1822. Begin
  1823. Consume(AS_SEPARATOR);
  1824. end;
  1825. AS_END:
  1826. begin
  1827. break; { end assembly block }
  1828. end;
  1829. else
  1830. Begin
  1831. Message(asmr_e_syntax_error);
  1832. RecoverConsume(false);
  1833. end;
  1834. end;
  1835. until false;
  1836. { Check LocalLabelList }
  1837. LocalLabelList^.CheckEmitted;
  1838. dispose(LocalLabelList,Done);
  1839. { are we back in the code section? }
  1840. if lastsec<>sec_code then
  1841. begin
  1842. Message(asmr_w_assembler_code_not_returned_to_text);
  1843. curlist^.Concat(new(pai_section,init(sec_code)));
  1844. end;
  1845. { Return the list in an asmnode }
  1846. assemble:=genasmnode(curlist);
  1847. Message1(asmr_d_finish_reading,'AT&T');
  1848. end;
  1849. {*****************************************************************************
  1850. Initialize
  1851. *****************************************************************************}
  1852. var
  1853. old_exit : pointer;
  1854. procedure ra386att_exit;{$ifndef FPC}far;{$endif}
  1855. begin
  1856. if assigned(iasmops) then
  1857. dispose(iasmops);
  1858. if assigned(iasmregs) then
  1859. dispose(iasmregs);
  1860. exitproc:=old_exit;
  1861. end;
  1862. begin
  1863. old_exit:=exitproc;
  1864. exitproc:=@ra386att_exit;
  1865. end.
  1866. {
  1867. $Log$
  1868. Revision 1.73 2000-04-04 13:48:44 pierre
  1869. + TOperand.SetCorrectSize virtual method added
  1870. to be able to change the suffix according to the instruction
  1871. (FIADD word ptr w need a s as ATT suffix
  1872. wheras FILD word ptr w need a w suffix :( )
  1873. Revision 1.72 2000/03/15 23:10:01 pierre
  1874. * fix for bug 848 (that still genrated wrong code)
  1875. + better testing for variables used in assembler
  1876. (gives an error if variable is not directly reachable !)
  1877. Revision 1.71 2000/02/09 13:23:01 peter
  1878. * log truncated
  1879. Revision 1.70 2000/01/28 09:41:39 peter
  1880. * fixed fpu suffix parsing for att reader
  1881. Revision 1.69 2000/01/21 10:10:25 daniel
  1882. * should work on linux also
  1883. Revision 1.68 2000/01/21 00:46:47 peter
  1884. * ifdef'd my previous fix as it broken a make cycle sometimes
  1885. Revision 1.67 2000/01/20 23:35:01 peter
  1886. * fixed fldl where suffix would get S_L instead of S_FL
  1887. Revision 1.66 2000/01/07 01:14:34 peter
  1888. * updated copyright to 2000
  1889. Revision 1.65 1999/12/12 12:57:59 peter
  1890. * allow para+offset
  1891. Revision 1.64 1999/11/30 10:40:52 peter
  1892. + ttype, tsymlist
  1893. Revision 1.63 1999/11/17 17:05:03 pierre
  1894. * Notes/hints changes
  1895. Revision 1.62 1999/11/09 23:06:46 peter
  1896. * esi_offset -> selfpointer_offset to be newcg compatible
  1897. * hcogegen -> cgbase fixes for newcg
  1898. Revision 1.61 1999/11/06 14:34:23 peter
  1899. * truncated log to 20 revs
  1900. Revision 1.60 1999/10/01 07:59:20 peter
  1901. * fixed object field parsing
  1902. Revision 1.59 1999/09/27 23:44:57 peter
  1903. * procinfo is now a pointer
  1904. * support for result setting in sub procedure
  1905. Revision 1.58 1999/09/08 16:04:01 peter
  1906. * better support for object fields and more error checks for
  1907. field accesses which create buggy code
  1908. Revision 1.57 1999/08/05 16:53:08 peter
  1909. * V_Fatal=1, all other V_ are also increased
  1910. * Check for local procedure when assigning procvar
  1911. * fixed comment parsing because directives
  1912. * oldtp mode directives better supported
  1913. * added some messages to errore.msg
  1914. Revision 1.56 1999/08/04 00:23:25 florian
  1915. * renamed i386asm and i386base to cpuasm and cpubase
  1916. Revision 1.55 1999/08/03 22:03:09 peter
  1917. * moved bitmask constants to sets
  1918. * some other type/const renamings
  1919. Revision 1.54 1999/07/24 11:17:12 peter
  1920. * suffix parsing for at&t fixed for things like movsbl
  1921. * string constants are now handle correctly and also allowed in
  1922. constant expressions
  1923. }