ra386att.pas 55 KB

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