ra386att.pas 54 KB

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