2
0

ra386att.pas 55 KB

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