ra386att.pas 54 KB

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