ra386att.pas 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. {
  2. $Id$
  3. Copyright (c) 1997-99 by Carl Eric Codere and Peter Vreman
  4. Does the parsing for the AT&T styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. {$ifdef TP}
  19. {$N+,E+}
  20. {$endif TP}
  21. Unit Ra386att;
  22. Interface
  23. uses
  24. tree;
  25. function assemble: ptree;
  26. Implementation
  27. Uses
  28. globtype,
  29. strings,cobjects,systems,verbose,globals,
  30. files,aasm,types,symtable,scanner,hcodegen
  31. ,i386base
  32. ,rautils,ra386;
  33. type
  34. tasmtoken = (
  35. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  36. AS_REALNUM,AS_COMMA,AS_LPAREN,
  37. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  38. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,AS_DOLLAR,
  39. {------------------ Assembler directives --------------------}
  40. AS_DB,AS_DW,AS_DD,AS_DQ,AS_GLOBAL,
  41. AS_ALIGN,AS_BALIGN,AS_P2ALIGN,AS_ASCII,
  42. AS_ASCIIZ,AS_LCOMM,AS_COMM,AS_SINGLE,AS_DOUBLE,AS_EXTENDED,
  43. AS_DATA,AS_TEXT,AS_END,
  44. {------------------ Assembler Operators --------------------}
  45. AS_MOD,AS_SHL,AS_SHR,AS_NOT,AS_AND,AS_OR,AS_XOR,AS_NOR);
  46. tasmkeyword = string[8];
  47. const
  48. { These tokens should be modified accordingly to the modifications }
  49. { in the different enumerations. }
  50. firstdirective = AS_DB;
  51. lastdirective = AS_END;
  52. _count_asmprefixes = 5;
  53. _count_asmspecialops = 25;
  54. _count_asmoverrides = 3;
  55. token2str : array[tasmtoken] of string[10]=(
  56. '','Label','LLabel','string','integer',
  57. 'float',',','(',
  58. ')',':','.','+','-','*',
  59. ';','identifier','register','opcode','/','$',
  60. '.byte','.word','.long','.quad','.globl',
  61. '.align','.balign','.p2align','.ascii',
  62. '.asciz','.lcomm','.comm','.single','.double','.tfloat',
  63. '.data','.text','END',
  64. '%','<<','>>','!','&','|','^','~');
  65. const
  66. newline = #10;
  67. firsttoken : boolean = TRUE;
  68. charcount : byte = 0;
  69. var
  70. _asmsorted,
  71. inexpression : boolean;
  72. curlist : paasmoutput;
  73. c : char;
  74. actasmtoken : tasmtoken;
  75. prevasmtoken : tasmtoken;
  76. actasmpattern : string;
  77. actopcode : tasmop;
  78. actasmregister : tregister;
  79. actopsize : topsize;
  80. actcondition : tasmcond;
  81. iasmops : ^op2strtable;
  82. iasmregs : ^reg2strtable;
  83. Procedure SetupTables;
  84. { creates uppercased symbol tables for speed access }
  85. var
  86. i : tasmop;
  87. j : tregister;
  88. Begin
  89. { opcodes }
  90. new(iasmops);
  91. for i:=firstop to lastop do
  92. iasmops^[i] := upper(att_op2str[i]);
  93. { registers }
  94. new(iasmregs);
  95. for j:=firstreg to lastreg do
  96. iasmregs^[j] := upper(att_reg2str[j]);
  97. end;
  98. {---------------------------------------------------------------------}
  99. { Routines for the tokenizing }
  100. {---------------------------------------------------------------------}
  101. function is_asmopcode(const s: string):boolean;
  102. const
  103. att_sizesuffixstr : array[0..8] of string[2] = (
  104. '','B','W','L','BW','BL','WL','Q','T'
  105. );
  106. att_sizesuffix : array[0..8] of topsize = (
  107. S_NO,S_B,S_W,S_L,S_BW,S_BL,S_WL,S_IQ,S_FX
  108. );
  109. var
  110. i : tasmop;
  111. cond : string[4];
  112. cnd : tasmcond;
  113. len,
  114. j,
  115. sufidx : longint;
  116. hid : string;
  117. Begin
  118. is_asmopcode:=FALSE;
  119. actopcode:=A_None;
  120. actcondition:=C_None;
  121. actopsize:=S_NO;
  122. { search for all possible suffixes }
  123. for sufidx:=low(att_sizesuffixstr) to high(att_sizesuffixstr) do
  124. begin
  125. len:=length(s)-length(att_sizesuffixstr[sufidx]);
  126. if copy(s,len+1,length(att_sizesuffixstr[sufidx]))=att_sizesuffixstr[sufidx] then
  127. begin
  128. { here we search the entire table... }
  129. hid:=copy(s,1,len);
  130. for i:=firstop to lastop do
  131. if (length(hid) > 0) and (hid=iasmops^[i]) then
  132. begin
  133. actopsize:=att_sizesuffix[sufidx];
  134. actopcode:=i;
  135. actasmtoken:=AS_OPCODE;
  136. is_asmopcode:=TRUE;
  137. exit;
  138. end;
  139. { not found, check condition opcodes }
  140. j:=0;
  141. while (j<CondAsmOps) do
  142. begin
  143. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  144. begin
  145. cond:=Copy(s,Length(CondAsmOpStr[j])+1,len-Length(CondAsmOpStr[j]));
  146. if cond<>'' then
  147. begin
  148. for cnd:=low(TasmCond) to high(TasmCond) do
  149. if Cond=Upper(cond2str[cnd]) then
  150. begin
  151. actopcode:=CondASmOp[j];
  152. actopsize:=att_sizesuffix[sufidx];
  153. actcondition:=cnd;
  154. actasmtoken:=AS_OPCODE;
  155. is_asmopcode:=TRUE;
  156. exit;
  157. end;
  158. end;
  159. end;
  160. inc(j);
  161. end;
  162. end;
  163. end;
  164. end;
  165. Function is_asmdirective(const s: string):boolean;
  166. var
  167. i : tasmtoken;
  168. hs : string;
  169. Begin
  170. { GNU as is also not casesensitive with this }
  171. hs:=lower(s);
  172. for i:=firstdirective to lastdirective do
  173. if hs=token2str[i] then
  174. begin
  175. actasmtoken:=i;
  176. is_asmdirective:=true;
  177. exit;
  178. end;
  179. is_asmdirective:=false;
  180. end;
  181. Function is_register(const s: string):boolean;
  182. Var
  183. i : tregister;
  184. Begin
  185. actasmregister:=R_NO;
  186. for i:=firstreg to lastreg do
  187. if s=iasmregs^[i] then
  188. begin
  189. actasmtoken:=AS_REGISTER;
  190. actasmregister:=i;
  191. is_register:=true;
  192. exit;
  193. end;
  194. is_register:=false;
  195. end;
  196. Function is_locallabel(const s: string):boolean;
  197. begin
  198. is_locallabel:=(length(s)>=2) and (s[1]='.') and (s[2]='L');
  199. end;
  200. Procedure GetToken;
  201. var
  202. forcelabel: boolean;
  203. errorflag : boolean;
  204. len : longint;
  205. begin
  206. { save old token and reset new token }
  207. prevasmtoken:=actasmtoken;
  208. actasmtoken:=AS_NONE;
  209. { reset }
  210. errorflag:=FALSE;
  211. forcelabel:=FALSE;
  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. inexpression:=TRUE;
  714. parenlevel:=0;
  715. Repeat
  716. Case actasmtoken of
  717. AS_LPAREN:
  718. Begin
  719. { Exit if ref? }
  720. if allowref and (prevasmtoken in [AS_INTNUM,AS_ID]) then
  721. break;
  722. Consume(AS_LPAREN);
  723. expr:=expr + '(';
  724. inc(parenlevel);
  725. end;
  726. AS_RPAREN:
  727. Begin
  728. { end of ref ? }
  729. if (parenlevel=0) and betweenbracket then
  730. break;
  731. Consume(AS_RPAREN);
  732. expr:=expr + ')';
  733. dec(parenlevel);
  734. end;
  735. AS_SHL:
  736. Begin
  737. Consume(AS_SHL);
  738. expr:=expr + '<';
  739. end;
  740. AS_SHR:
  741. Begin
  742. Consume(AS_SHR);
  743. expr:=expr + '>';
  744. end;
  745. AS_SLASH:
  746. Begin
  747. Consume(AS_SLASH);
  748. expr:=expr + '/';
  749. end;
  750. AS_MOD:
  751. Begin
  752. Consume(AS_MOD);
  753. expr:=expr + '%';
  754. end;
  755. AS_STAR:
  756. Begin
  757. Consume(AS_STAR);
  758. expr:=expr + '*';
  759. end;
  760. AS_PLUS:
  761. Begin
  762. Consume(AS_PLUS);
  763. expr:=expr + '+';
  764. end;
  765. AS_MINUS:
  766. Begin
  767. Consume(AS_MINUS);
  768. expr:=expr + '-';
  769. end;
  770. AS_AND:
  771. Begin
  772. Consume(AS_AND);
  773. expr:=expr + '&';
  774. end;
  775. AS_NOT:
  776. Begin
  777. Consume(AS_NOT);
  778. expr:=expr + '~';
  779. end;
  780. AS_XOR:
  781. Begin
  782. Consume(AS_XOR);
  783. expr:=expr + '^';
  784. end;
  785. AS_OR:
  786. Begin
  787. Consume(AS_OR);
  788. expr:=expr + '|';
  789. end;
  790. AS_INTNUM:
  791. Begin
  792. expr:=expr + actasmpattern;
  793. Consume(AS_INTNUM);
  794. end;
  795. AS_DOLLAR:
  796. begin
  797. Consume(AS_DOLLAR);
  798. if actasmtoken<>AS_ID then
  799. Comment(V_Error,'assem_e_dollar_without_identifier');
  800. end;
  801. AS_ID:
  802. Begin
  803. tempstr:=actasmpattern;
  804. prevtok:=prevasmtoken;
  805. consume(AS_ID);
  806. if actasmtoken=AS_DOT then
  807. begin
  808. BuildRecordOffsetSize(tempstr,l,k);
  809. str(l, tempstr);
  810. expr:=expr + tempstr;
  811. end
  812. else
  813. if SearchIConstant(tempstr,l) then
  814. begin
  815. str(l, tempstr);
  816. expr:=expr + tempstr;
  817. end
  818. else
  819. begin
  820. hs:='';
  821. if is_locallabel(tempstr) then
  822. begin
  823. CreateLocalLabel(tempstr,hl,false);
  824. hs:=hl^.name
  825. end
  826. else
  827. if SearchLabel(tempstr,hl,false) then
  828. hs:=hl^.name
  829. else
  830. begin
  831. getsym(tempstr,false);
  832. sym:=srsym;
  833. if assigned(sym) then
  834. begin
  835. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  836. Message(asmr_e_no_local_or_para_allowed);
  837. case srsym^.typ of
  838. varsym :
  839. hs:=pvarsym(srsym)^.mangledname;
  840. typedconstsym :
  841. hs:=ptypedconstsym(srsym)^.mangledname;
  842. procsym :
  843. hs:=pprocsym(srsym)^.mangledname;
  844. else
  845. Message(asmr_e_wrong_sym_type);
  846. end;
  847. end
  848. else
  849. Message1(sym_e_unknown_id,tempstr);
  850. end;
  851. { symbol found? }
  852. if hs<>'' then
  853. begin
  854. if needofs and (prevtok<>AS_DOLLAR) then
  855. Message(asmr_e_need_offset);
  856. if asmsym='' then
  857. asmsym:=hs
  858. else
  859. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  860. if (expr='') or (expr[length(expr)]='+') then
  861. begin
  862. delete(expr,length(expr),1);
  863. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  864. Message(asmr_e_only_add_relocatable_symbol);
  865. end
  866. else
  867. Message(asmr_e_only_add_relocatable_symbol);
  868. end
  869. else
  870. begin
  871. { Error recovery }
  872. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  873. delete(expr,length(expr),1);
  874. end;
  875. end;
  876. end;
  877. AS_END,
  878. AS_SEPARATOR,
  879. AS_COMMA:
  880. Begin
  881. break;
  882. end;
  883. else
  884. Begin
  885. { write error only once. }
  886. if not errorflag then
  887. Message(asmr_e_invalid_constant_expression);
  888. { consume tokens until we find COMMA or SEPARATOR }
  889. Consume(actasmtoken);
  890. errorflag:=TRUE;
  891. end;
  892. end;
  893. Until false;
  894. { calculate expression }
  895. if not ErrorFlag then
  896. value:=CalculateExpression(expr)
  897. else
  898. value:=0;
  899. { no longer in an expression }
  900. inexpression:=FALSE;
  901. end;
  902. Function BuildConstExpression(allowref,betweenbracket:boolean): longint;
  903. var
  904. l : longint;
  905. hs : string;
  906. begin
  907. BuildConstSymbolExpression(allowref,betweenbracket,false,l,hs);
  908. if hs<>'' then
  909. Message(asmr_e_relocatable_symbol_not_allowed);
  910. BuildConstExpression:=l;
  911. end;
  912. {****************************************************************************
  913. T386ATTOperand
  914. ****************************************************************************}
  915. type
  916. P386ATTOperand=^T386ATTOperand;
  917. T386ATTOperand=object(T386Operand)
  918. Procedure BuildOperand;virtual;
  919. private
  920. Procedure BuildReference;
  921. Procedure BuildConstant;
  922. end;
  923. Procedure T386ATTOperand.BuildReference;
  924. procedure Consume_RParen;
  925. begin
  926. if actasmtoken <> AS_RPAREN then
  927. Begin
  928. Message(asmr_e_invalid_reference_syntax);
  929. RecoverConsume(true);
  930. end
  931. else
  932. begin
  933. Consume(AS_RPAREN);
  934. if not (actasmtoken in [AS_COMMA,AS_SEPARATOR,AS_END]) then
  935. Begin
  936. Message(asmr_e_invalid_reference_syntax);
  937. RecoverConsume(true);
  938. end;
  939. end;
  940. end;
  941. procedure Consume_Scale;
  942. var
  943. l : longint;
  944. begin
  945. { we have to process the scaling }
  946. l:=BuildConstExpression(false,true);
  947. if ((l = 2) or (l = 4) or (l = 8) or (l = 1)) then
  948. opr.ref.scalefactor:=l
  949. else
  950. Begin
  951. Message(asmr_e_wrong_scale_factor);
  952. opr.ref.scalefactor:=0;
  953. end;
  954. end;
  955. Begin
  956. Consume(AS_LPAREN);
  957. Case actasmtoken of
  958. AS_INTNUM,
  959. AS_MINUS,
  960. AS_PLUS: { absolute offset, such as fs:(0x046c) }
  961. Begin
  962. { offset(offset) is invalid }
  963. If opr.Ref.Offset <> 0 Then
  964. Begin
  965. Message(asmr_e_invalid_reference_syntax);
  966. RecoverConsume(true);
  967. End
  968. Else
  969. Begin
  970. opr.Ref.Offset:=BuildConstExpression(false,true);
  971. Consume_RParen;
  972. end;
  973. exit;
  974. End;
  975. AS_REGISTER: { (reg ... }
  976. Begin
  977. { Check if there is already a base (mostly ebp,esp) than this is
  978. not allowed,becuase it will give crashing code }
  979. if opr.ref.base<>R_NO then
  980. Message(asmr_e_cannot_index_relative_var);
  981. opr.ref.base:=actasmregister;
  982. Consume(AS_REGISTER);
  983. { can either be a register or a right parenthesis }
  984. { (reg) }
  985. if actasmtoken=AS_RPAREN then
  986. Begin
  987. Consume_RParen;
  988. exit;
  989. end;
  990. { (reg,reg .. }
  991. Consume(AS_COMMA);
  992. if actasmtoken=AS_REGISTER then
  993. Begin
  994. opr.ref.index:=actasmregister;
  995. Consume(AS_REGISTER);
  996. { check for scaling ... }
  997. case actasmtoken of
  998. AS_RPAREN:
  999. Begin
  1000. Consume_RParen;
  1001. exit;
  1002. end;
  1003. AS_COMMA:
  1004. Begin
  1005. Consume(AS_COMMA);
  1006. Consume_Scale;
  1007. Consume_RParen;
  1008. end;
  1009. else
  1010. Begin
  1011. Message(asmr_e_invalid_reference_syntax);
  1012. RecoverConsume(false);
  1013. end;
  1014. end; { end case }
  1015. end
  1016. else
  1017. Begin
  1018. Message(asmr_e_invalid_reference_syntax);
  1019. RecoverConsume(false);
  1020. end;
  1021. end; {end case }
  1022. AS_COMMA: { (, ... can either be scaling, or index }
  1023. Begin
  1024. Consume(AS_COMMA);
  1025. { Index }
  1026. if (actasmtoken=AS_REGISTER) then
  1027. Begin
  1028. opr.ref.index:=actasmregister;
  1029. Consume(AS_REGISTER);
  1030. { check for scaling ... }
  1031. case actasmtoken of
  1032. AS_RPAREN:
  1033. Begin
  1034. Consume_RParen;
  1035. exit;
  1036. end;
  1037. AS_COMMA:
  1038. Begin
  1039. Consume(AS_COMMA);
  1040. Consume_Scale;
  1041. Consume_RParen;
  1042. end;
  1043. else
  1044. Begin
  1045. Message(asmr_e_invalid_reference_syntax);
  1046. RecoverConsume(false);
  1047. end;
  1048. end; {end case }
  1049. end
  1050. { Scaling }
  1051. else
  1052. Begin
  1053. Consume_Scale;
  1054. Consume_RParen;
  1055. exit;
  1056. end;
  1057. end;
  1058. else
  1059. Begin
  1060. Message(asmr_e_invalid_reference_syntax);
  1061. RecoverConsume(false);
  1062. end;
  1063. end;
  1064. end;
  1065. Procedure T386ATTOperand.BuildConstant;
  1066. var
  1067. l : longint;
  1068. tempstr : string;
  1069. begin
  1070. BuildConstSymbolExpression(false,false,true,l,tempstr);
  1071. if tempstr<>'' then
  1072. begin
  1073. opr.typ:=OPR_SYMBOL;
  1074. opr.symofs:=l;
  1075. opr.symbol:=newasmsymbol(tempstr);
  1076. end
  1077. else
  1078. begin
  1079. opr.typ:=OPR_CONSTANT;
  1080. opr.val:=l;
  1081. end;
  1082. end;
  1083. Procedure T386ATTOperand.BuildOperand;
  1084. procedure AddLabelOperand(hl:pasmlabel);
  1085. begin
  1086. if not(actasmtoken in [AS_PLUS,AS_MINUS,AS_LPAREN]) and
  1087. is_calljmp(actopcode) then
  1088. begin
  1089. opr.typ:=OPR_SYMBOL;
  1090. opr.symbol:=hl;
  1091. end
  1092. else
  1093. begin
  1094. InitRef;
  1095. opr.ref.symbol:=hl;
  1096. end;
  1097. end;
  1098. function MaybeBuildReference:boolean;
  1099. { Try to create a reference, if not a reference is found then false
  1100. is returned }
  1101. begin
  1102. MaybeBuildReference:=true;
  1103. case actasmtoken of
  1104. AS_INTNUM,
  1105. AS_MINUS,
  1106. AS_PLUS:
  1107. Begin
  1108. opr.ref.offset:=BuildConstExpression(True,False);
  1109. if actasmtoken<>AS_LPAREN then
  1110. Message(asmr_e_invalid_reference_syntax)
  1111. else
  1112. BuildReference;
  1113. end;
  1114. AS_LPAREN:
  1115. BuildReference;
  1116. AS_ID: { only a variable is allowed ... }
  1117. Begin
  1118. if not SetupVar(actasmpattern) then
  1119. Message(asmr_e_invalid_reference_syntax);
  1120. Consume(actasmtoken);
  1121. case actasmtoken of
  1122. AS_END,
  1123. AS_SEPARATOR,
  1124. AS_COMMA: ;
  1125. AS_LPAREN: BuildReference;
  1126. else
  1127. Begin
  1128. Message(asmr_e_invalid_reference_syntax);
  1129. Consume(actasmtoken);
  1130. end;
  1131. end; {end case }
  1132. end;
  1133. else
  1134. MaybeBuildReference:=false;
  1135. end; { end case }
  1136. end;
  1137. var
  1138. expr,
  1139. tempstr : string;
  1140. tempreg : tregister;
  1141. hl : PAsmLabel;
  1142. tsize,l,
  1143. toffset : longint;
  1144. Begin
  1145. tempstr:='';
  1146. expr:='';
  1147. case actasmtoken of
  1148. AS_LPAREN: { Memory reference or constant expression }
  1149. Begin
  1150. InitRef;
  1151. BuildReference;
  1152. end;
  1153. AS_DOLLAR: { Constant expression }
  1154. Begin
  1155. Consume(AS_DOLLAR);
  1156. BuildConstant;
  1157. end;
  1158. AS_INTNUM,
  1159. AS_MINUS,
  1160. AS_PLUS:
  1161. Begin
  1162. { Constant memory offset }
  1163. { This must absolutely be followed by ( }
  1164. InitRef;
  1165. opr.ref.offset:=BuildConstExpression(True,False);
  1166. if actasmtoken<>AS_LPAREN then
  1167. Message(asmr_e_invalid_reference_syntax)
  1168. else
  1169. BuildReference;
  1170. end;
  1171. AS_STAR: { Call from memory address }
  1172. Begin
  1173. Consume(AS_STAR);
  1174. if actasmtoken=AS_REGISTER then
  1175. begin
  1176. opr.typ:=OPR_REGISTER;
  1177. opr.reg:=actasmregister;
  1178. size:=reg_2_opsize[actasmregister];
  1179. Consume(AS_REGISTER);
  1180. end
  1181. else
  1182. begin
  1183. InitRef;
  1184. if not MaybeBuildReference then
  1185. Message(asmr_e_syn_operand);
  1186. end;
  1187. { this is only allowed for call's and jmp's }
  1188. if not is_calljmp(actopcode) then
  1189. Message(asmr_e_syn_operand);
  1190. end;
  1191. AS_ID: { A constant expression, or a Variable ref. }
  1192. Begin
  1193. { Local Label ? }
  1194. if is_locallabel(actasmpattern) then
  1195. begin
  1196. CreateLocalLabel(actasmpattern,hl,false);
  1197. Consume(AS_ID);
  1198. AddLabelOperand(hl);
  1199. end
  1200. else
  1201. { Check for label }
  1202. if SearchLabel(actasmpattern,hl,false) then
  1203. begin
  1204. Consume(AS_ID);
  1205. AddLabelOperand(hl);
  1206. end
  1207. else
  1208. { probably a variable or normal expression }
  1209. { or a procedure (such as in CALL ID) }
  1210. Begin
  1211. InitRef;
  1212. if not SetupVar(actasmpattern) then
  1213. Begin
  1214. { look for special symbols ... }
  1215. if actasmpattern = '__RESULT' then
  1216. SetUpResult
  1217. else
  1218. if actasmpattern = '__SELF' then
  1219. SetupSelf
  1220. else
  1221. if actasmpattern = '__OLDEBP' then
  1222. SetupOldEBP
  1223. else
  1224. { check for direct symbolic names }
  1225. { only if compiling the system unit }
  1226. if (cs_compilesystem in aktmoduleswitches) then
  1227. begin
  1228. if not SetupDirectVar(actasmpattern) then
  1229. Begin
  1230. { not found, finally ... add it anyways ... }
  1231. Message1(asmr_w_id_supposed_external,actasmpattern);
  1232. opr.ref.symbol:=newasmsymbol(actasmpattern);
  1233. end;
  1234. end
  1235. else
  1236. Message1(sym_e_unknown_id,actasmpattern);
  1237. end;
  1238. { constant expression? }
  1239. if (opr.typ=OPR_CONSTANT) then
  1240. begin
  1241. l:=BuildConstExpression(true,false);
  1242. { indexing? }
  1243. if actasmtoken=AS_LPAREN then
  1244. begin
  1245. opr.typ:=OPR_REFERENCE;
  1246. reset_reference(opr.Ref);
  1247. opr.Ref.Offset:=l;
  1248. BuildReference;
  1249. end
  1250. else
  1251. opr.Val:=l;
  1252. end
  1253. else
  1254. begin
  1255. expr:=actasmpattern;
  1256. Consume(AS_ID);
  1257. if actasmtoken=AS_DOT then
  1258. begin
  1259. BuildRecordOffsetSize(expr,toffset,tsize);
  1260. inc(opr.ref.offset,toffset);
  1261. SetSize(tsize);
  1262. end;
  1263. end;
  1264. end;
  1265. if opr.typ=OPR_REFERENCE then
  1266. begin
  1267. { Do we have a +[constant] ? }
  1268. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1269. inc(opr.ref.offset,BuildConstExpression(true,false));
  1270. { Do we have a indexing reference, then parse it also }
  1271. if actasmtoken=AS_LPAREN then
  1272. BuildReference;
  1273. end;
  1274. end;
  1275. AS_REGISTER: { Register, a variable reference or a constant reference }
  1276. Begin
  1277. { save the type of register used. }
  1278. tempreg:=actasmregister;
  1279. Consume(AS_REGISTER);
  1280. if actasmtoken = AS_COLON then
  1281. Begin
  1282. Consume(AS_COLON);
  1283. InitRef;
  1284. opr.ref.segment:=tempreg;
  1285. { This must absolutely be followed by a reference }
  1286. if not MaybeBuildReference then
  1287. Begin
  1288. Message(asmr_e_invalid_seg_override);
  1289. Consume(actasmtoken);
  1290. end;
  1291. end
  1292. { Simple register }
  1293. else if (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1294. Begin
  1295. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1296. Message(asmr_e_invalid_operand_type);
  1297. opr.typ:=OPR_REGISTER;
  1298. opr.reg:=tempreg;
  1299. size:=reg_2_opsize[tempreg];
  1300. end
  1301. else
  1302. Message(asmr_e_syn_operand);
  1303. end;
  1304. AS_END,
  1305. AS_SEPARATOR,
  1306. AS_COMMA: ;
  1307. else
  1308. Begin
  1309. Message(asmr_e_syn_operand);
  1310. Consume(actasmtoken);
  1311. end;
  1312. end; { end case }
  1313. end;
  1314. {*****************************************************************************
  1315. T386ATTInstruction
  1316. *****************************************************************************}
  1317. type
  1318. P386AttInstruction=^T386AttInstruction;
  1319. T386AttInstruction=object(T386Instruction)
  1320. procedure InitOperands;virtual;
  1321. procedure BuildOpcode;virtual;
  1322. end;
  1323. procedure T386AttInstruction.InitOperands;
  1324. var
  1325. i : longint;
  1326. begin
  1327. for i:=1to 3 do
  1328. Operands[i]:=new(P386AttOperand,Init);
  1329. end;
  1330. Procedure T386AttInstruction.BuildOpCode;
  1331. var
  1332. operandnum : longint;
  1333. PrefixOp,OverrideOp: tasmop;
  1334. expr : string;
  1335. Begin
  1336. expr:='';
  1337. PrefixOp:=A_None;
  1338. OverrideOp:=A_None;
  1339. { prefix seg opcode / prefix opcode }
  1340. repeat
  1341. if is_prefix(actopcode) then
  1342. begin
  1343. PrefixOp:=ActOpcode;
  1344. opcode:=ActOpcode;
  1345. condition:=ActCondition;
  1346. opsize:=ActOpsize;
  1347. ConcatInstruction(curlist);
  1348. Consume(AS_OPCODE);
  1349. end
  1350. else
  1351. if is_override(actopcode) then
  1352. begin
  1353. OverrideOp:=ActOpcode;
  1354. opcode:=ActOpcode;
  1355. condition:=ActCondition;
  1356. opsize:=ActOpsize;
  1357. ConcatInstruction(curlist);
  1358. Consume(AS_OPCODE);
  1359. end
  1360. else
  1361. break;
  1362. { allow for newline as in gas styled syntax }
  1363. while actasmtoken=AS_SEPARATOR do
  1364. Consume(AS_SEPARATOR);
  1365. until (actasmtoken<>AS_OPCODE);
  1366. { opcode }
  1367. if (actasmtoken <> AS_OPCODE) then
  1368. Begin
  1369. Message(asmr_e_invalid_or_missing_opcode);
  1370. RecoverConsume(true);
  1371. exit;
  1372. end;
  1373. { Fill the instr object with the current state }
  1374. Opcode:=ActOpcode;
  1375. condition:=ActCondition;
  1376. opsize:=ActOpsize;
  1377. { Valid combination of prefix/override and instruction ? }
  1378. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1379. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1380. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1381. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1382. { We are reading operands, so opcode will be an AS_ID }
  1383. operandnum:=1;
  1384. Consume(AS_OPCODE);
  1385. { Zero operand opcode ? }
  1386. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1387. begin
  1388. operandnum:=0;
  1389. exit;
  1390. end;
  1391. { Read the operands }
  1392. repeat
  1393. case actasmtoken of
  1394. AS_COMMA: { Operand delimiter }
  1395. Begin
  1396. if operandnum > MaxOperands then
  1397. Message(asmr_e_too_many_operands)
  1398. else
  1399. Inc(operandnum);
  1400. Consume(AS_COMMA);
  1401. end;
  1402. AS_SEPARATOR,
  1403. AS_END : { End of asm operands for this opcode }
  1404. begin
  1405. break;
  1406. end;
  1407. else
  1408. Operands[operandnum]^.BuildOperand;
  1409. end; { end case }
  1410. until false;
  1411. Ops:=operandnum;
  1412. end;
  1413. Procedure BuildConstant(maxvalue: longint);
  1414. var
  1415. strlength: byte;
  1416. asmsym,
  1417. expr: string;
  1418. value : longint;
  1419. Begin
  1420. Repeat
  1421. Case actasmtoken of
  1422. AS_STRING:
  1423. Begin
  1424. if maxvalue = $ff then
  1425. strlength:=1
  1426. else
  1427. Message(asmr_e_string_not_allowed_as_const);
  1428. expr:=actasmpattern;
  1429. if length(expr) > 1 then
  1430. Message(asmr_e_string_not_allowed_as_const);
  1431. Consume(AS_STRING);
  1432. Case actasmtoken of
  1433. AS_COMMA: Consume(AS_COMMA);
  1434. AS_END,
  1435. AS_SEPARATOR: ;
  1436. else
  1437. Message(asmr_e_invalid_string_expression);
  1438. end; { end case }
  1439. ConcatString(curlist,expr);
  1440. end;
  1441. AS_INTNUM,
  1442. AS_PLUS,
  1443. AS_MINUS,
  1444. AS_LPAREN,
  1445. AS_NOT,
  1446. AS_ID :
  1447. Begin
  1448. BuildConstSymbolExpression(false,false,false,value,asmsym);
  1449. if asmsym<>'' then
  1450. begin
  1451. if maxvalue<>$ffffffff then
  1452. Comment(V_Warning,'32bit constant created for address');
  1453. ConcatConstSymbol(curlist,asmsym,value)
  1454. end
  1455. else
  1456. ConcatConstant(curlist,value,maxvalue);
  1457. end;
  1458. AS_COMMA:
  1459. Consume(AS_COMMA);
  1460. AS_END,
  1461. AS_SEPARATOR:
  1462. break;
  1463. else
  1464. begin
  1465. Message(asmr_e_syn_constant);
  1466. RecoverConsume(false);
  1467. end
  1468. end; { end case }
  1469. Until false;
  1470. end;
  1471. Procedure BuildRealConstant(typ : tfloattype);
  1472. var
  1473. expr : string;
  1474. r : bestreal;
  1475. code : integer;
  1476. negativ : boolean;
  1477. errorflag: boolean;
  1478. Begin
  1479. errorflag:=FALSE;
  1480. Repeat
  1481. negativ:=false;
  1482. expr:='';
  1483. if actasmtoken=AS_PLUS then
  1484. Consume(AS_PLUS)
  1485. else
  1486. if actasmtoken=AS_MINUS then
  1487. begin
  1488. negativ:=true;
  1489. consume(AS_MINUS);
  1490. end;
  1491. Case actasmtoken of
  1492. AS_INTNUM:
  1493. Begin
  1494. expr:=actasmpattern;
  1495. Consume(AS_INTNUM);
  1496. if negativ then
  1497. expr:='-'+expr;
  1498. val(expr,r,code);
  1499. if code<>0 then
  1500. Begin
  1501. r:=0;
  1502. Message(asmr_e_invalid_float_expr);
  1503. End;
  1504. ConcatRealConstant(curlist,r,typ);
  1505. end;
  1506. AS_REALNUM:
  1507. Begin
  1508. expr:=actasmpattern;
  1509. Consume(AS_REALNUM);
  1510. { in ATT syntax you have 0d in front of the real }
  1511. { should this be forced ? yes i think so, as to }
  1512. { conform to gas as much as possible. }
  1513. if (expr[1]='0') and (upper(expr[2])='D') then
  1514. Delete(expr,1,2);
  1515. if negativ then
  1516. expr:='-'+expr;
  1517. val(expr,r,code);
  1518. if code<>0 then
  1519. Begin
  1520. r:=0;
  1521. Message(asmr_e_invalid_float_expr);
  1522. End;
  1523. ConcatRealConstant(curlist,r,typ);
  1524. end;
  1525. AS_COMMA:
  1526. begin
  1527. Consume(AS_COMMA);
  1528. end;
  1529. AS_END,
  1530. AS_SEPARATOR:
  1531. begin
  1532. break;
  1533. end;
  1534. else
  1535. Begin
  1536. Consume(actasmtoken);
  1537. if not errorflag then
  1538. Message(asmr_e_invalid_float_expr);
  1539. errorflag:=TRUE;
  1540. end;
  1541. end;
  1542. Until false;
  1543. end;
  1544. Procedure BuildStringConstant(asciiz: boolean);
  1545. var
  1546. expr: string;
  1547. errorflag : boolean;
  1548. Begin
  1549. errorflag:=FALSE;
  1550. Repeat
  1551. Case actasmtoken of
  1552. AS_STRING:
  1553. Begin
  1554. expr:=actasmpattern;
  1555. if asciiz then
  1556. expr:=expr+#0;
  1557. ConcatPasString(curlist,expr);
  1558. Consume(AS_STRING);
  1559. end;
  1560. AS_COMMA:
  1561. begin
  1562. Consume(AS_COMMA);
  1563. end;
  1564. AS_END,
  1565. AS_SEPARATOR:
  1566. begin
  1567. break;
  1568. end;
  1569. else
  1570. Begin
  1571. Consume(actasmtoken);
  1572. if not errorflag then
  1573. Message(asmr_e_invalid_string_expression);
  1574. errorflag:=TRUE;
  1575. end;
  1576. end;
  1577. Until false;
  1578. end;
  1579. Function Assemble: Ptree;
  1580. Var
  1581. hl : PAsmLabel;
  1582. commname : string;
  1583. lastsec : tsection;
  1584. l1,l2 : longint;
  1585. instr : T386ATTInstruction;
  1586. Begin
  1587. Message1(asmr_d_start_reading,'AT&T');
  1588. firsttoken:=TRUE;
  1589. if assigned(procinfo.retdef) and
  1590. (is_fpu(procinfo.retdef) or
  1591. ret_in_acc(procinfo.retdef)) then
  1592. procinfo.funcret_is_valid:=true;
  1593. { sets up all opcode and register tables in uppercase }
  1594. if not _asmsorted then
  1595. Begin
  1596. SetupTables;
  1597. _asmsorted:=TRUE;
  1598. end;
  1599. curlist:=new(paasmoutput,init);
  1600. lastsec:=sec_code;
  1601. { setup label linked list }
  1602. new(LocalLabelList,Init);
  1603. { start tokenizer }
  1604. c:=current_scanner^.asmgetchar;
  1605. gettoken;
  1606. { main loop }
  1607. repeat
  1608. case actasmtoken of
  1609. AS_LLABEL:
  1610. Begin
  1611. if CreateLocalLabel(actasmpattern,hl,true) then
  1612. ConcatLabel(curlist,hl);
  1613. Consume(AS_LLABEL);
  1614. end;
  1615. AS_LABEL:
  1616. Begin
  1617. if SearchLabel(upper(actasmpattern),hl,true) then
  1618. ConcatLabel(curlist,hl)
  1619. else
  1620. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1621. Consume(AS_LABEL);
  1622. end;
  1623. AS_DW:
  1624. Begin
  1625. Consume(AS_DW);
  1626. BuildConstant($ffff);
  1627. end;
  1628. AS_DATA:
  1629. Begin
  1630. curlist^.Concat(new(pai_section,init(sec_data)));
  1631. lastsec:=sec_data;
  1632. Consume(AS_DATA);
  1633. end;
  1634. AS_TEXT:
  1635. Begin
  1636. curlist^.Concat(new(pai_section,init(sec_code)));
  1637. lastsec:=sec_code;
  1638. Consume(AS_TEXT);
  1639. end;
  1640. AS_DB:
  1641. Begin
  1642. Consume(AS_DB);
  1643. BuildConstant($ff);
  1644. end;
  1645. AS_DD:
  1646. Begin
  1647. Consume(AS_DD);
  1648. BuildConstant($ffffffff);
  1649. end;
  1650. AS_DQ:
  1651. Begin
  1652. Consume(AS_DQ);
  1653. BuildRealConstant(s64comp);
  1654. end;
  1655. AS_SINGLE:
  1656. Begin
  1657. Consume(AS_SINGLE);
  1658. BuildRealConstant(s32real);
  1659. end;
  1660. AS_DOUBLE:
  1661. Begin
  1662. Consume(AS_DOUBLE);
  1663. BuildRealConstant(s64real);
  1664. end;
  1665. AS_EXTENDED:
  1666. Begin
  1667. Consume(AS_EXTENDED);
  1668. BuildRealConstant(s80real);
  1669. end;
  1670. AS_GLOBAL:
  1671. Begin
  1672. Consume(AS_GLOBAL);
  1673. if actasmtoken=AS_ID then
  1674. ConcatPublic(curlist,actasmpattern);
  1675. Consume(AS_ID);
  1676. if actasmtoken<>AS_SEPARATOR then
  1677. Consume(AS_SEPARATOR);
  1678. end;
  1679. AS_ALIGN:
  1680. Begin
  1681. Consume(AS_ALIGN);
  1682. l1:=BuildConstExpression(false,false);
  1683. if (target_info.target in [target_i386_GO32V1,target_i386_GO32V2]) then
  1684. begin
  1685. l2:=1;
  1686. if (l1>=0) and (l1<=16) then
  1687. while (l1>0) do
  1688. begin
  1689. l2:=2*l2;
  1690. dec(l1);
  1691. end;
  1692. l1:=l2;
  1693. end;
  1694. ConcatAlign(curlist,l1);
  1695. Comment(V_Note,'.align is target specific, use .balign or .p2align');
  1696. if actasmtoken<>AS_SEPARATOR then
  1697. Consume(AS_SEPARATOR);
  1698. end;
  1699. AS_BALIGN:
  1700. Begin
  1701. Consume(AS_BALIGN);
  1702. ConcatAlign(curlist,BuildConstExpression(false,false));
  1703. if actasmtoken<>AS_SEPARATOR then
  1704. Consume(AS_SEPARATOR);
  1705. end;
  1706. AS_P2ALIGN:
  1707. Begin
  1708. Consume(AS_P2ALIGN);
  1709. l1:=BuildConstExpression(false,false);
  1710. l2:=1;
  1711. if (l1>=0) and (l1<=16) then
  1712. while (l1>0) do
  1713. begin
  1714. l2:=2*l2;
  1715. dec(l1);
  1716. end;
  1717. l1:=l2;
  1718. ConcatAlign(curlist,l1);
  1719. if actasmtoken<>AS_SEPARATOR then
  1720. Consume(AS_SEPARATOR);
  1721. end;
  1722. AS_ASCIIZ:
  1723. Begin
  1724. Consume(AS_ASCIIZ);
  1725. BuildStringConstant(TRUE);
  1726. end;
  1727. AS_ASCII:
  1728. Begin
  1729. Consume(AS_ASCII);
  1730. BuildStringConstant(FALSE);
  1731. end;
  1732. AS_LCOMM:
  1733. Begin
  1734. Consume(AS_LCOMM);
  1735. commname:=actasmpattern;
  1736. Consume(AS_ID);
  1737. Consume(AS_COMMA);
  1738. ConcatLocalBss(commname,BuildConstExpression(false,false));
  1739. if actasmtoken<>AS_SEPARATOR then
  1740. Consume(AS_SEPARATOR);
  1741. end;
  1742. AS_COMM:
  1743. Begin
  1744. Consume(AS_COMM);
  1745. commname:=actasmpattern;
  1746. Consume(AS_ID);
  1747. Consume(AS_COMMA);
  1748. ConcatGlobalBss(commname,BuildConstExpression(false,false));
  1749. if actasmtoken<>AS_SEPARATOR then
  1750. Consume(AS_SEPARATOR);
  1751. end;
  1752. AS_OPCODE:
  1753. Begin
  1754. instr.init;
  1755. instr.BuildOpcode;
  1756. instr.AddReferenceSizes;
  1757. instr.SetInstructionOpsize;
  1758. instr.CheckOperandSizes;
  1759. instr.ConcatInstruction(curlist);
  1760. instr.done;
  1761. end;
  1762. AS_SEPARATOR:
  1763. Begin
  1764. Consume(AS_SEPARATOR);
  1765. end;
  1766. AS_END:
  1767. begin
  1768. break; { end assembly block }
  1769. end;
  1770. else
  1771. Begin
  1772. Message(asmr_e_syntax_error);
  1773. RecoverConsume(false);
  1774. end;
  1775. end;
  1776. until false;
  1777. { Check LocalLabelList }
  1778. LocalLabelList^.CheckEmitted;
  1779. dispose(LocalLabelList,Done);
  1780. { are we back in the code section? }
  1781. if lastsec<>sec_code then
  1782. begin
  1783. Message(asmr_w_assembler_code_not_returned_to_text);
  1784. curlist^.Concat(new(pai_section,init(sec_code)));
  1785. end;
  1786. { Return the list in an asmnode }
  1787. assemble:=genasmnode(curlist);
  1788. Message1(asmr_d_finish_reading,'AT&T');
  1789. end;
  1790. {*****************************************************************************
  1791. Initialize
  1792. *****************************************************************************}
  1793. var
  1794. old_exit : pointer;
  1795. procedure ra386att_exit;{$ifndef FPC}far;{$endif}
  1796. begin
  1797. if assigned(iasmops) then
  1798. dispose(iasmops);
  1799. if assigned(iasmregs) then
  1800. dispose(iasmregs);
  1801. exitproc:=old_exit;
  1802. end;
  1803. begin
  1804. old_exit:=exitproc;
  1805. exitproc:=@ra386att_exit;
  1806. end.
  1807. {
  1808. $Log$
  1809. Revision 1.53 1999-06-21 16:45:01 peter
  1810. * merged
  1811. Revision 1.52 1999/06/14 17:48:03 peter
  1812. * merged
  1813. Revision 1.50.2.2 1999/06/21 16:42:57 peter
  1814. * fixed label and opcode on the same line
  1815. Revision 1.50.2.1 1999/06/14 17:30:44 peter
  1816. * align fixes from pierre
  1817. Revision 1.51 1999/06/11 22:54:12 pierre
  1818. * .align problem treated :
  1819. .align is considered as .p2align on go32v1 and go32v2
  1820. and as .balign on other targets
  1821. + ra386att supports also .balign and .p2align
  1822. * ag386att uses .balign allways
  1823. Revision 1.50 1999/06/08 11:51:58 peter
  1824. * fixed some intel bugs with scale parsing
  1825. * end is now also a separator in many more cases
  1826. Revision 1.49 1999/06/03 16:28:03 pierre
  1827. * typo corrected
  1828. Revision 1.48 1999/05/27 19:44:56 peter
  1829. * removed oldasm
  1830. * plabel -> pasmlabel
  1831. * -a switches to source writing automaticly
  1832. * assembler readers OOPed
  1833. * asmsymbol automaticly external
  1834. * jumptables and other label fixes for asm readers
  1835. Revision 1.47 1999/05/21 13:55:13 peter
  1836. * NEWLAB for label as symbol
  1837. Revision 1.46 1999/05/12 00:19:56 peter
  1838. * removed R_DEFAULT_SEG
  1839. * uniform float names
  1840. Revision 1.45 1999/05/06 09:05:25 peter
  1841. * generic write_float and str_float
  1842. * fixed constant float conversions
  1843. Revision 1.44 1999/05/05 22:22:00 peter
  1844. * updated messages
  1845. Revision 1.43 1999/05/04 21:45:01 florian
  1846. * changes to compile it with Delphi 4.0
  1847. Revision 1.42 1999/05/02 14:25:07 peter
  1848. * only allow *<reg/ref> when call/jmp is used
  1849. Revision 1.41 1999/05/01 13:48:39 peter
  1850. * merged nasm compiler
  1851. Revision 1.7 1999/04/29 09:37:47 peter
  1852. * fixed var+const support
  1853. Revision 1.6 1999/04/26 23:26:17 peter
  1854. * redesigned record offset parsing to support nested records
  1855. * normal compiler uses the redesigned createvarinstr()
  1856. Revision 1.5 1999/04/20 11:01:23 peter
  1857. * better tokenpos info
  1858. Revision 1.4 1999/04/14 09:07:45 peter
  1859. * asm reader improvements
  1860. Revision 1.3 1999/03/06 17:24:26 peter
  1861. * rewritten intel parser a lot, especially reference reading
  1862. * size checking added for asm parsers
  1863. Revision 1.2 1999/03/02 02:56:30 peter
  1864. + stabs support for binary writers
  1865. * more fixes and missing updates from the previous commit :(
  1866. Revision 1.1 1999/03/01 15:46:26 peter
  1867. * ag386bin finally make cycles correct
  1868. * prefixes are now also normal opcodes
  1869. }