ra386int.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. {
  2. $Id$
  3. Copyright (c) 1997-99 by Carl Eric Codere and Peter Vreman
  4. Does the parsing process for the intel 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. {$E+,N+}
  20. {$endif}
  21. Unit Ra386int;
  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,scanner,hcodegen,symtable,cpubase,
  31. rautils,ra386;
  32. type
  33. tasmtoken = (
  34. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  35. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  36. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  37. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  38. {------------------ Assembler directives --------------------}
  39. AS_DB,AS_DW,AS_DD,AS_END,
  40. {------------------ Assembler Operators --------------------}
  41. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_NEAR,AS_FAR,
  42. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  43. AS_AND,AS_OR,AS_XOR);
  44. tasmkeyword = string[6];
  45. const
  46. { These tokens should be modified accordingly to the modifications }
  47. { in the different enumerations. }
  48. firstdirective = AS_DB;
  49. lastdirective = AS_END;
  50. firstoperator = AS_BYTE;
  51. lastoperator = AS_XOR;
  52. firstsreg = R_CS;
  53. lastsreg = R_SS;
  54. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  55. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  56. _count_asmprefixes = 5;
  57. _count_asmspecialops = 25;
  58. _count_asmoverrides = 3;
  59. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  60. ('DB','DW','DD','END');
  61. { problems with shl,shr,not,and,or and xor, they are }
  62. { context sensitive. }
  63. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  64. 'BYTE','WORD','DWORD','QWORD','TBYTE','NEAR','FAR','HIGH',
  65. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  66. 'OR','XOR');
  67. token2str : array[tasmtoken] of string[10] = (
  68. '','Label','LLabel','String','Integer',
  69. ',','[',']','(',
  70. ')',':','.','+','-','*',
  71. ';','identifier','register','opcode','/',
  72. '','','','END',
  73. '','','','','','','','',
  74. '','','','type','ptr','mod','shl','shr','not',
  75. 'and','or','xor'
  76. );
  77. const
  78. newline = #10;
  79. firsttoken : boolean = TRUE;
  80. var
  81. _asmsorted : boolean;
  82. inexpression : boolean;
  83. curlist : paasmoutput;
  84. c : char;
  85. prevasmtoken : tasmtoken;
  86. actasmtoken : tasmtoken;
  87. actasmpattern : string;
  88. actasmregister : tregister;
  89. actopcode : tasmop;
  90. actopsize : topsize;
  91. actcondition : tasmcond;
  92. iasmops : ^op2strtable;
  93. iasmregs : ^reg2strtable;
  94. Procedure SetupTables;
  95. { creates uppercased symbol tables for speed access }
  96. var
  97. i : tasmop;
  98. j : tregister;
  99. Begin
  100. { opcodes }
  101. new(iasmops);
  102. for i:=firstop to lastop do
  103. iasmops^[i] := upper(int_op2str[i]);
  104. { registers }
  105. new(iasmregs);
  106. for j:=firstreg to lastreg do
  107. iasmregs^[j] := upper(int_reg2str[j]);
  108. end;
  109. {---------------------------------------------------------------------}
  110. { Routines for the tokenizing }
  111. {---------------------------------------------------------------------}
  112. function is_asmopcode(const s: string):boolean;
  113. var
  114. i: tasmop;
  115. cond : string[4];
  116. cnd : tasmcond;
  117. j: longint;
  118. Begin
  119. is_asmopcode:=FALSE;
  120. actopcode:=A_None;
  121. actcondition:=C_None;
  122. actopsize:=S_NO;
  123. for i:=firstop to lastop do
  124. if s=iasmops^[i] then
  125. begin
  126. actopcode:=i;
  127. actasmtoken:=AS_OPCODE;
  128. is_asmopcode:=TRUE;
  129. exit;
  130. end;
  131. { not found yet, check condition opcodes }
  132. j:=0;
  133. while (j<CondAsmOps) do
  134. begin
  135. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  136. begin
  137. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  138. if cond<>'' then
  139. begin
  140. for cnd:=low(TasmCond) to high(TasmCond) do
  141. if Cond=Upper(cond2str[cnd]) then
  142. begin
  143. actopcode:=CondASmOp[j];
  144. actcondition:=cnd;
  145. is_asmopcode:=TRUE;
  146. actasmtoken:=AS_OPCODE;
  147. exit
  148. end;
  149. end;
  150. end;
  151. inc(j);
  152. end;
  153. end;
  154. function is_asmoperator(const s: string):boolean;
  155. var
  156. i : longint;
  157. Begin
  158. for i:=0 to _count_asmoperators do
  159. if s=_asmoperators[i] then
  160. begin
  161. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  162. is_asmoperator:=true;
  163. exit;
  164. end;
  165. is_asmoperator:=false;
  166. end;
  167. Function is_asmdirective(const s: string):boolean;
  168. var
  169. i : longint;
  170. Begin
  171. for i:=0 to _count_asmdirectives do
  172. if s=_asmdirectives[i] then
  173. begin
  174. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  175. is_asmdirective:=true;
  176. exit;
  177. end;
  178. is_asmdirective:=false;
  179. end;
  180. Function is_register(const s: string):boolean;
  181. Var
  182. i : tregister;
  183. Begin
  184. actasmregister:=R_NO;
  185. for i:=firstreg to lastreg do
  186. if s=iasmregs^[i] then
  187. begin
  188. actasmtoken:=AS_REGISTER;
  189. actasmregister:=i;
  190. is_register:=true;
  191. exit;
  192. end;
  193. is_register:=false;
  194. end;
  195. function is_locallabel(const s:string):boolean;
  196. begin
  197. is_locallabel:=(length(s)>1) and (s[1]='@');
  198. end;
  199. Procedure GetToken;
  200. var
  201. len : longint;
  202. forcelabel : boolean;
  203. begin
  204. { save old token and reset new token }
  205. prevasmtoken:=actasmtoken;
  206. actasmtoken:=AS_NONE;
  207. { reset }
  208. forcelabel:=FALSE;
  209. actasmpattern:='';
  210. { while space and tab , continue scan... }
  211. while (c in [' ',#9]) do
  212. c:=current_scanner^.asmgetchar;
  213. { get token pos }
  214. if not (c in [newline,#13,'{',';']) then
  215. current_scanner^.gettokenpos;
  216. { Local Label, Label, Directive, Prefix or Opcode }
  217. if firsttoken and not (c in [newline,#13,'{',';']) then
  218. begin
  219. firsttoken:=FALSE;
  220. len:=0;
  221. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  222. begin
  223. { if there is an at_sign, then this must absolutely be a label }
  224. if c = '@' then
  225. forcelabel:=TRUE;
  226. inc(len);
  227. actasmpattern[len]:=c;
  228. c:=current_scanner^.asmgetchar;
  229. end;
  230. actasmpattern[0]:=chr(len);
  231. uppervar(actasmpattern);
  232. { label ? }
  233. if c = ':' then
  234. begin
  235. if actasmpattern[1]='@' then
  236. actasmtoken:=AS_LLABEL
  237. else
  238. actasmtoken:=AS_LABEL;
  239. { let us point to the next character }
  240. c:=current_scanner^.asmgetchar;
  241. firsttoken:=true;
  242. exit;
  243. end;
  244. { Are we trying to create an identifier with }
  245. { an at-sign...? }
  246. if forcelabel then
  247. Message(asmr_e_none_label_contain_at);
  248. { opcode ? }
  249. If is_asmopcode(actasmpattern) then
  250. Begin
  251. { check if we are in an expression }
  252. { then continue with asm directives }
  253. if not inexpression then
  254. exit;
  255. end;
  256. if is_asmdirective(actasmpattern) then
  257. exit;
  258. actasmtoken:=AS_NONE;
  259. exit;
  260. end
  261. else { else firsttoken }
  262. begin
  263. case c of
  264. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  265. { - @Result, @Code or @Data special variables. }
  266. begin
  267. actasmpattern:=c;
  268. c:=current_scanner^.asmgetchar;
  269. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  270. begin
  271. actasmpattern:=actasmpattern + c;
  272. c:=current_scanner^.asmgetchar;
  273. end;
  274. uppervar(actasmpattern);
  275. actasmtoken:=AS_ID;
  276. exit;
  277. end;
  278. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  279. begin
  280. actasmpattern:=c;
  281. c:=current_scanner^.asmgetchar;
  282. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  283. begin
  284. actasmpattern:=actasmpattern + c;
  285. c:=current_scanner^.asmgetchar;
  286. end;
  287. uppervar(actasmpattern);
  288. { after prefix we allow also a new opcode }
  289. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  290. Begin
  291. { if we are not in a constant }
  292. { expression than this is an }
  293. { opcode. }
  294. if not inexpression then
  295. exit;
  296. end;
  297. { support st(X) for fpu registers }
  298. if (actasmpattern = 'ST') and (c='(') then
  299. Begin
  300. actasmpattern:=actasmpattern+c;
  301. c:=current_scanner^.asmgetchar;
  302. if c in ['0'..'9'] then
  303. actasmpattern:=actasmpattern + c
  304. else
  305. Message(asmr_e_invalid_fpu_register);
  306. c:=current_scanner^.asmgetchar;
  307. if c <> ')' then
  308. Message(asmr_e_invalid_fpu_register)
  309. else
  310. Begin
  311. actasmpattern:=actasmpattern + c;
  312. c:=current_scanner^.asmgetchar;
  313. end;
  314. end;
  315. if is_register(actasmpattern) then
  316. exit;
  317. if is_asmdirective(actasmpattern) then
  318. exit;
  319. if is_asmoperator(actasmpattern) then
  320. exit;
  321. actasmtoken:=AS_ID;
  322. exit;
  323. end;
  324. '&' : { override operator... not supported }
  325. begin
  326. Message(asmr_w_override_op_not_supported);
  327. c:=current_scanner^.asmgetchar;
  328. actasmtoken:=AS_NONE;
  329. end;
  330. '''' : { string or character }
  331. begin
  332. actasmpattern:='';
  333. repeat
  334. if c = '''' then
  335. begin
  336. c:=current_scanner^.asmgetchar;
  337. if c=newline then
  338. begin
  339. Message(scan_f_string_exceeds_line);
  340. break;
  341. end;
  342. repeat
  343. if c='''' then
  344. begin
  345. c:=current_scanner^.asmgetchar;
  346. if c='''' then
  347. begin
  348. actasmpattern:=actasmpattern+'''';
  349. c:=current_scanner^.asmgetchar;
  350. if c=newline then
  351. begin
  352. Message(scan_f_string_exceeds_line);
  353. break;
  354. end;
  355. end
  356. else
  357. break;
  358. end
  359. else
  360. begin
  361. actasmpattern:=actasmpattern+c;
  362. c:=current_scanner^.asmgetchar;
  363. if c=newline then
  364. begin
  365. Message(scan_f_string_exceeds_line);
  366. break
  367. end;
  368. end;
  369. until false; { end repeat }
  370. end
  371. else
  372. break; { end if }
  373. until false;
  374. actasmtoken:=AS_STRING;
  375. exit;
  376. end;
  377. '"' : { string or character }
  378. begin
  379. actasmpattern:='';
  380. repeat
  381. if c = '"' then
  382. begin
  383. c:=current_scanner^.asmgetchar;
  384. if c=newline then
  385. begin
  386. Message(scan_f_string_exceeds_line);
  387. break;
  388. end;
  389. repeat
  390. if c='"' then
  391. begin
  392. c:=current_scanner^.asmgetchar;
  393. if c='"' then
  394. begin
  395. actasmpattern:=actasmpattern+'"';
  396. c:=current_scanner^.asmgetchar;
  397. if c=newline then
  398. begin
  399. Message(scan_f_string_exceeds_line);
  400. break;
  401. end;
  402. end
  403. else
  404. break;
  405. end
  406. else
  407. begin
  408. actasmpattern:=actasmpattern+c;
  409. c:=current_scanner^.asmgetchar;
  410. if c=newline then
  411. begin
  412. Message(scan_f_string_exceeds_line);
  413. break
  414. end;
  415. end;
  416. until false; { end repeat }
  417. end
  418. else
  419. break; { end if }
  420. until false;
  421. actasmtoken:=AS_STRING;
  422. exit;
  423. end;
  424. '$' :
  425. begin
  426. c:=current_scanner^.asmgetchar;
  427. while c in ['0'..'9','A'..'F','a'..'f'] do
  428. begin
  429. actasmpattern:=actasmpattern + c;
  430. c:=current_scanner^.asmgetchar;
  431. end;
  432. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  433. actasmtoken:=AS_INTNUM;
  434. exit;
  435. end;
  436. ',' :
  437. begin
  438. actasmtoken:=AS_COMMA;
  439. c:=current_scanner^.asmgetchar;
  440. exit;
  441. end;
  442. '[' :
  443. begin
  444. actasmtoken:=AS_LBRACKET;
  445. c:=current_scanner^.asmgetchar;
  446. exit;
  447. end;
  448. ']' :
  449. begin
  450. actasmtoken:=AS_RBRACKET;
  451. c:=current_scanner^.asmgetchar;
  452. exit;
  453. end;
  454. '(' :
  455. begin
  456. actasmtoken:=AS_LPAREN;
  457. c:=current_scanner^.asmgetchar;
  458. exit;
  459. end;
  460. ')' :
  461. begin
  462. actasmtoken:=AS_RPAREN;
  463. c:=current_scanner^.asmgetchar;
  464. exit;
  465. end;
  466. ':' :
  467. begin
  468. actasmtoken:=AS_COLON;
  469. c:=current_scanner^.asmgetchar;
  470. exit;
  471. end;
  472. '.' :
  473. begin
  474. actasmtoken:=AS_DOT;
  475. c:=current_scanner^.asmgetchar;
  476. exit;
  477. end;
  478. '+' :
  479. begin
  480. actasmtoken:=AS_PLUS;
  481. c:=current_scanner^.asmgetchar;
  482. exit;
  483. end;
  484. '-' :
  485. begin
  486. actasmtoken:=AS_MINUS;
  487. c:=current_scanner^.asmgetchar;
  488. exit;
  489. end;
  490. '*' :
  491. begin
  492. actasmtoken:=AS_STAR;
  493. c:=current_scanner^.asmgetchar;
  494. exit;
  495. end;
  496. '/' :
  497. begin
  498. actasmtoken:=AS_SLASH;
  499. c:=current_scanner^.asmgetchar;
  500. exit;
  501. end;
  502. '0'..'9':
  503. begin
  504. actasmpattern:=c;
  505. c:=current_scanner^.asmgetchar;
  506. { Get the possible characters }
  507. while c in ['0'..'9','A'..'F','a'..'f'] do
  508. begin
  509. actasmpattern:=actasmpattern + c;
  510. c:=current_scanner^.asmgetchar;
  511. end;
  512. { Get ending character }
  513. uppervar(actasmpattern);
  514. c:=upcase(c);
  515. { possibly a binary number. }
  516. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  517. Begin
  518. { Delete the last binary specifier }
  519. delete(actasmpattern,length(actasmpattern),1);
  520. actasmpattern:=tostr(ValBinary(actasmpattern));
  521. actasmtoken:=AS_INTNUM;
  522. exit;
  523. end
  524. else
  525. Begin
  526. case c of
  527. 'O' :
  528. Begin
  529. actasmpattern:=tostr(ValOctal(actasmpattern));
  530. actasmtoken:=AS_INTNUM;
  531. c:=current_scanner^.asmgetchar;
  532. exit;
  533. end;
  534. 'H' :
  535. Begin
  536. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  537. actasmtoken:=AS_INTNUM;
  538. c:=current_scanner^.asmgetchar;
  539. exit;
  540. end;
  541. else { must be an integer number }
  542. begin
  543. actasmpattern:=tostr(ValDecimal(actasmpattern));
  544. actasmtoken:=AS_INTNUM;
  545. exit;
  546. end;
  547. end;
  548. end;
  549. end;
  550. ';','{',#13,newline :
  551. begin
  552. c:=current_scanner^.asmgetchar;
  553. firsttoken:=TRUE;
  554. actasmtoken:=AS_SEPARATOR;
  555. exit;
  556. end;
  557. else
  558. Begin
  559. Message(scan_f_illegal_char);
  560. end;
  561. end;
  562. end;
  563. end;
  564. function consume(t : tasmtoken):boolean;
  565. begin
  566. Consume:=true;
  567. if t<>actasmtoken then
  568. begin
  569. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  570. Consume:=false;
  571. end;
  572. repeat
  573. gettoken;
  574. until actasmtoken<>AS_NONE;
  575. end;
  576. procedure RecoverConsume(allowcomma:boolean);
  577. begin
  578. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  579. begin
  580. if allowcomma and (actasmtoken=AS_COMMA) then
  581. break;
  582. Consume(actasmtoken);
  583. end;
  584. end;
  585. {*****************************************************************************
  586. Parsing Helpers
  587. *****************************************************************************}
  588. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  589. { Description: This routine builds up a record offset after a AS_DOT }
  590. { token is encountered. }
  591. { On entry actasmtoken should be equal to AS_DOT }
  592. var
  593. s : string;
  594. Begin
  595. offset:=0;
  596. size:=0;
  597. s:=expr;
  598. while (actasmtoken=AS_DOT) do
  599. begin
  600. Consume(AS_DOT);
  601. if actasmtoken=AS_ID then
  602. s:=s+'.'+actasmpattern;
  603. if not Consume(AS_ID) then
  604. begin
  605. RecoverConsume(true);
  606. break;
  607. end;
  608. end;
  609. if not GetRecordOffsetSize(s,offset,size) then
  610. Message(asmr_e_building_record_offset);
  611. end;
  612. Procedure BuildConstSymbolExpression(needofs,exitreg:boolean;var value:longint;var asmsym:string);
  613. var
  614. tempstr,expr,hs : string;
  615. parenlevel,l,k : longint;
  616. errorflag : boolean;
  617. prevtok : tasmtoken;
  618. hl : PAsmLabel;
  619. sym : psym;
  620. Begin
  621. { reset }
  622. value:=0;
  623. asmsym:='';
  624. errorflag:=FALSE;
  625. tempstr:='';
  626. expr:='';
  627. inexpression:=TRUE;
  628. parenlevel:=0;
  629. Repeat
  630. Case actasmtoken of
  631. AS_LPAREN:
  632. Begin
  633. Consume(AS_LPAREN);
  634. expr:=expr + '(';
  635. inc(parenlevel);
  636. end;
  637. AS_RPAREN:
  638. Begin
  639. Consume(AS_RPAREN);
  640. expr:=expr + ')';
  641. dec(parenlevel);
  642. end;
  643. AS_SHL:
  644. Begin
  645. Consume(AS_SHL);
  646. expr:=expr + '<';
  647. end;
  648. AS_SHR:
  649. Begin
  650. Consume(AS_SHR);
  651. expr:=expr + '>';
  652. end;
  653. AS_SLASH:
  654. Begin
  655. Consume(AS_SLASH);
  656. expr:=expr + '/';
  657. end;
  658. AS_MOD:
  659. Begin
  660. Consume(AS_MOD);
  661. expr:=expr + '%';
  662. end;
  663. AS_STAR:
  664. Begin
  665. Consume(AS_STAR);
  666. if exitreg and (actasmtoken=AS_REGISTER) then
  667. break;
  668. expr:=expr + '*';
  669. end;
  670. AS_PLUS:
  671. Begin
  672. Consume(AS_PLUS);
  673. if exitreg and (actasmtoken=AS_REGISTER) then
  674. break;
  675. expr:=expr + '+';
  676. end;
  677. AS_MINUS:
  678. Begin
  679. Consume(AS_MINUS);
  680. expr:=expr + '-';
  681. end;
  682. AS_AND:
  683. Begin
  684. Consume(AS_AND);
  685. expr:=expr + '&';
  686. end;
  687. AS_NOT:
  688. Begin
  689. Consume(AS_NOT);
  690. expr:=expr + '~';
  691. end;
  692. AS_XOR:
  693. Begin
  694. Consume(AS_XOR);
  695. expr:=expr + '^';
  696. end;
  697. AS_OR:
  698. Begin
  699. Consume(AS_OR);
  700. expr:=expr + '|';
  701. end;
  702. AS_INTNUM:
  703. Begin
  704. expr:=expr + actasmpattern;
  705. Consume(AS_INTNUM);
  706. end;
  707. AS_OFFSET:
  708. begin
  709. Consume(AS_OFFSET);
  710. if actasmtoken<>AS_ID then
  711. Message(asmr_e_offset_without_identifier);
  712. end;
  713. AS_STRING:
  714. Begin
  715. l:=0;
  716. case Length(actasmpattern) of
  717. 1 :
  718. l:=ord(actasmpattern[1]);
  719. 2 :
  720. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  721. 3 :
  722. l:=ord(actasmpattern[3]) +
  723. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  724. 4 :
  725. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  726. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  727. else
  728. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  729. end;
  730. str(l, tempstr);
  731. expr:=expr + tempstr;
  732. Consume(AS_STRING);
  733. end;
  734. AS_ID:
  735. Begin
  736. tempstr:=actasmpattern;
  737. prevtok:=prevasmtoken;
  738. consume(AS_ID);
  739. if actasmtoken=AS_DOT then
  740. begin
  741. BuildRecordOffsetSize(tempstr,l,k);
  742. str(l, tempstr);
  743. expr:=expr + tempstr;
  744. end
  745. else
  746. if SearchIConstant(tempstr,l) then
  747. begin
  748. str(l, tempstr);
  749. expr:=expr + tempstr;
  750. end
  751. else
  752. begin
  753. hs:='';
  754. if is_locallabel(tempstr) then
  755. begin
  756. CreateLocalLabel(tempstr,hl,false);
  757. hs:=hl^.name
  758. end
  759. else
  760. if SearchLabel(tempstr,hl,false) then
  761. hs:=hl^.name
  762. else
  763. begin
  764. getsym(tempstr,false);
  765. sym:=srsym;
  766. if assigned(sym) then
  767. begin
  768. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  769. Message(asmr_e_no_local_or_para_allowed);
  770. case srsym^.typ of
  771. varsym :
  772. hs:=pvarsym(srsym)^.mangledname;
  773. typedconstsym :
  774. hs:=ptypedconstsym(srsym)^.mangledname;
  775. procsym :
  776. hs:=pprocsym(srsym)^.mangledname;
  777. else
  778. Message(asmr_e_wrong_sym_type);
  779. end;
  780. end
  781. else
  782. Message1(sym_e_unknown_id,tempstr);
  783. end;
  784. { symbol found? }
  785. if hs<>'' then
  786. begin
  787. if needofs and (prevtok<>AS_OFFSET) then
  788. Message(asmr_e_need_offset);
  789. if asmsym='' then
  790. asmsym:=hs
  791. else
  792. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  793. if (expr='') or (expr[length(expr)]='+') then
  794. begin
  795. delete(expr,length(expr),1);
  796. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  797. Message(asmr_e_only_add_relocatable_symbol);
  798. end
  799. else
  800. Message(asmr_e_only_add_relocatable_symbol);
  801. end;
  802. end;
  803. end;
  804. AS_END,
  805. AS_RBRACKET,
  806. AS_SEPARATOR,
  807. AS_COMMA:
  808. Begin
  809. break;
  810. end;
  811. else
  812. Begin
  813. { write error only once. }
  814. if not errorflag then
  815. Message(asmr_e_invalid_constant_expression);
  816. { consume tokens until we find COMMA or SEPARATOR }
  817. Consume(actasmtoken);
  818. errorflag:=TRUE;
  819. end;
  820. end;
  821. Until false;
  822. { calculate expression }
  823. if not ErrorFlag then
  824. value:=CalculateExpression(expr)
  825. else
  826. value:=0;
  827. { no longer in an expression }
  828. inexpression:=FALSE;
  829. end;
  830. Function BuildConstExpression:longint;
  831. var
  832. l : longint;
  833. hs : string;
  834. begin
  835. BuildConstSymbolExpression(false,false,l,hs);
  836. if hs<>'' then
  837. Message(asmr_e_relocatable_symbol_not_allowed);
  838. BuildConstExpression:=l;
  839. end;
  840. Function BuildRefConstExpression:longint;
  841. var
  842. l : longint;
  843. hs : string;
  844. begin
  845. BuildConstSymbolExpression(false,true,l,hs);
  846. if hs<>'' then
  847. Message(asmr_e_relocatable_symbol_not_allowed);
  848. BuildRefConstExpression:=l;
  849. end;
  850. {****************************************************************************
  851. T386IntelOperand
  852. ****************************************************************************}
  853. type
  854. P386IntelOperand=^T386IntelOperand;
  855. T386IntelOperand=object(T386Operand)
  856. Procedure BuildOperand;virtual;
  857. private
  858. Procedure BuildReference;
  859. Procedure BuildConstant;
  860. end;
  861. Procedure T386IntelOperand.BuildReference;
  862. var
  863. l : longint;
  864. hs : string;
  865. code : integer;
  866. hreg,
  867. oldbase : tregister;
  868. GotStar,
  869. GotPlus,Negative : boolean;
  870. Begin
  871. Consume(AS_LBRACKET);
  872. InitRef;
  873. GotStar:=false;
  874. GotPlus:=true;
  875. Negative:=false;
  876. repeat
  877. Case actasmtoken of
  878. AS_ID: { Constant reference expression OR variable reference expression }
  879. Begin
  880. if not GotPlus then
  881. Message(asmr_e_invalid_reference_syntax);
  882. if actasmpattern[1] = '@' then
  883. Message(asmr_e_local_symbol_not_allowed_as_ref);
  884. GotStar:=false;
  885. GotPlus:=false;
  886. if SearchIConstant(actasmpattern,l) or
  887. SearchRecordType(actasmpattern) then
  888. begin
  889. l:=BuildRefConstExpression;
  890. GotPlus:=(prevasmtoken=AS_PLUS);
  891. GotStar:=(prevasmtoken=AS_STAR);
  892. if GotStar then
  893. opr.ref.scalefactor:=l
  894. else
  895. begin
  896. if negative then
  897. Dec(opr.ref.offset,l)
  898. else
  899. Inc(opr.ref.offset,l);
  900. end;
  901. end
  902. else
  903. Begin
  904. if hasvar then
  905. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  906. if negative then
  907. Message(asmr_e_only_add_relocatable_symbol);
  908. oldbase:=opr.ref.base;
  909. opr.ref.base:=R_NO;
  910. if not SetupVar(actasmpattern) then
  911. Message1(sym_e_unknown_id,actasmpattern);
  912. { is the base register loaded by the var ? }
  913. if (opr.ref.base<>R_NO) then
  914. begin
  915. { check if we can move the old base to the index register }
  916. if (opr.ref.index<>R_NO) then
  917. Message(asmr_e_wrong_base_index)
  918. else
  919. opr.ref.index:=oldbase;
  920. end
  921. else
  922. opr.ref.base:=oldbase;
  923. { we can't have a Constant here so add the constant value to the
  924. offset }
  925. if opr.typ=OPR_CONSTANT then
  926. begin
  927. opr.typ:=OPR_REFERENCE;
  928. inc(opr.ref.offset,opr.val);
  929. end;
  930. Consume(AS_ID);
  931. end;
  932. end;
  933. AS_PLUS :
  934. Begin
  935. Consume(AS_PLUS);
  936. Negative:=false;
  937. GotPlus:=true;
  938. GotStar:=false;
  939. end;
  940. AS_MINUS :
  941. begin
  942. Consume(AS_MINUS);
  943. Negative:=true;
  944. GotPlus:=true;
  945. GotStar:=false;
  946. end;
  947. AS_STAR : { Scaling, with eax*4 order }
  948. begin
  949. Consume(AS_STAR);
  950. hs:='';
  951. l:=0;
  952. case actasmtoken of
  953. AS_LPAREN :
  954. l:=BuildConstExpression;
  955. AS_INTNUM:
  956. Begin
  957. hs:=actasmpattern;
  958. Consume(AS_INTNUM);
  959. end;
  960. AS_REGISTER :
  961. begin
  962. if opr.ref.scalefactor=0 then
  963. Message(asmr_e_wrong_scale_factor);
  964. end;
  965. else
  966. Message(asmr_e_invalid_reference_syntax);
  967. end;
  968. if actasmtoken<>AS_REGISTER then
  969. begin
  970. if hs<>'' then
  971. val(hs,l,code);
  972. opr.ref.scalefactor:=l
  973. end;
  974. GotPlus:=false;
  975. GotStar:=false;
  976. end;
  977. AS_REGISTER :
  978. begin
  979. if (not GotPlus) and (not GotStar) then
  980. Message(asmr_e_invalid_reference_syntax);
  981. hreg:=actasmregister;
  982. Consume(AS_REGISTER);
  983. { this register will be the index:
  984. 1. just read a *
  985. 2. next token is a *
  986. 3. base register is already used }
  987. if (GotStar) or
  988. (actasmtoken=AS_STAR) or
  989. (opr.ref.base<>R_NO) then
  990. begin
  991. if (opr.ref.index<>R_NO) then
  992. Message(asmr_e_multiple_index);
  993. opr.ref.index:=hreg;
  994. end
  995. else
  996. opr.ref.base:=hreg;
  997. GotPlus:=false;
  998. GotStar:=false;
  999. end;
  1000. AS_NOT,
  1001. AS_INTNUM,
  1002. AS_LPAREN : { Constant reference expression }
  1003. begin
  1004. if not GotPlus then
  1005. Message(asmr_e_invalid_reference_syntax);
  1006. l:=BuildRefConstExpression;
  1007. GotPlus:=(prevasmtoken=AS_PLUS);
  1008. GotStar:=(prevasmtoken=AS_STAR);
  1009. if GotStar then
  1010. opr.ref.scalefactor:=l
  1011. else
  1012. begin
  1013. if negative then
  1014. Dec(opr.ref.offset,l)
  1015. else
  1016. Inc(opr.ref.offset,l);
  1017. end;
  1018. end;
  1019. AS_RBRACKET :
  1020. begin
  1021. if GotPlus then
  1022. Message(asmr_e_invalid_reference_syntax);
  1023. Consume(AS_RBRACKET);
  1024. break;
  1025. end;
  1026. else
  1027. Begin
  1028. Message(asmr_e_invalid_reference_syntax);
  1029. RecoverConsume(true);
  1030. break;
  1031. end;
  1032. end;
  1033. until false;
  1034. end;
  1035. Procedure T386IntelOperand.BuildConstant;
  1036. var
  1037. l : longint;
  1038. tempstr : string;
  1039. begin
  1040. BuildConstSymbolExpression(true,false,l,tempstr);
  1041. if tempstr<>'' then
  1042. begin
  1043. opr.typ:=OPR_SYMBOL;
  1044. opr.symofs:=l;
  1045. opr.symbol:=newasmsymbol(tempstr);
  1046. end
  1047. else
  1048. begin
  1049. opr.typ:=OPR_CONSTANT;
  1050. opr.val:=l;
  1051. end;
  1052. end;
  1053. Procedure T386IntelOperand.BuildOperand;
  1054. var
  1055. expr,
  1056. tempstr : string;
  1057. tempreg : tregister;
  1058. l : longint;
  1059. hl : PAsmLabel;
  1060. procedure AddLabelOperand(hl:pasmlabel);
  1061. begin
  1062. if is_calljmp(actopcode) then
  1063. begin
  1064. opr.typ:=OPR_SYMBOL;
  1065. opr.symbol:=hl;
  1066. end
  1067. else
  1068. begin
  1069. InitRef;
  1070. opr.ref.symbol:=hl;
  1071. end;
  1072. end;
  1073. procedure MaybeRecordOffset;
  1074. var
  1075. l,
  1076. toffset,
  1077. tsize : longint;
  1078. begin
  1079. l:=0;
  1080. if actasmtoken=AS_DOT then
  1081. begin
  1082. { if no type was specified before the [] then we expect the
  1083. first ID to be the type }
  1084. if expr='' then
  1085. begin
  1086. consume(AS_DOT);
  1087. if actasmtoken=AS_ID then
  1088. begin
  1089. expr:=actasmpattern;
  1090. consume(AS_ID);
  1091. { now the next one must the be the dot }
  1092. if actasmtoken<>AS_DOT then
  1093. begin
  1094. Message(asmr_e_building_record_offset);
  1095. expr:='';
  1096. end;
  1097. end
  1098. else
  1099. Message(asmr_e_no_var_type_specified)
  1100. end;
  1101. if expr<>'' then
  1102. begin
  1103. BuildRecordOffsetSize(expr,toffset,tsize);
  1104. inc(l,toffset);
  1105. SetSize(tsize);
  1106. end;
  1107. end;
  1108. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1109. inc(l,BuildConstExpression);
  1110. if opr.typ=OPR_REFERENCE then
  1111. inc(opr.ref.offset,l)
  1112. else
  1113. inc(opr.val,l);
  1114. end;
  1115. Begin
  1116. tempstr:='';
  1117. expr:='';
  1118. case actasmtoken of
  1119. AS_OFFSET,
  1120. AS_INTNUM,
  1121. AS_PLUS,
  1122. AS_MINUS,
  1123. AS_NOT,
  1124. AS_LPAREN,
  1125. AS_STRING :
  1126. Begin
  1127. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1128. Message(asmr_e_invalid_operand_type);
  1129. BuildConstant;
  1130. end;
  1131. AS_ID : { A constant expression, or a Variable ref. }
  1132. Begin
  1133. { Label or Special symbol reference? }
  1134. if actasmpattern[1] = '@' then
  1135. Begin
  1136. if actasmpattern = '@RESULT' then
  1137. Begin
  1138. InitRef;
  1139. SetupResult;
  1140. Consume(AS_ID);
  1141. end
  1142. else
  1143. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1144. begin
  1145. Message(asmr_w_CODE_and_DATA_not_supported);
  1146. Consume(AS_ID);
  1147. end
  1148. else
  1149. { Local Label }
  1150. begin
  1151. CreateLocalLabel(actasmpattern,hl,false);
  1152. Consume(AS_ID);
  1153. AddLabelOperand(hl);
  1154. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1155. Message(asmr_e_syntax_error);
  1156. end;
  1157. end
  1158. else
  1159. { support result for delphi modes }
  1160. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1161. begin
  1162. InitRef;
  1163. SetUpResult;
  1164. Consume(AS_ID);
  1165. end
  1166. { probably a variable or normal expression }
  1167. { or a procedure (such as in CALL ID) }
  1168. else
  1169. Begin
  1170. { is it a constant ? }
  1171. if SearchIConstant(actasmpattern,l) then
  1172. Begin
  1173. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1174. Message(asmr_e_invalid_operand_type);
  1175. BuildConstant;
  1176. end
  1177. else
  1178. { Check for pascal label }
  1179. if SearchLabel(actasmpattern,hl,false) then
  1180. begin
  1181. Consume(AS_ID);
  1182. AddLabelOperand(hl);
  1183. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1184. Message(asmr_e_syntax_error);
  1185. end
  1186. else
  1187. { is it a normal variable ? }
  1188. Begin
  1189. InitRef;
  1190. if not SetupVar(actasmpattern) then
  1191. Begin
  1192. { not a variable, check special variables.. }
  1193. if actasmpattern = 'SELF' then
  1194. SetupSelf
  1195. else
  1196. Message1(sym_e_unknown_id,actasmpattern);
  1197. end;
  1198. expr:=actasmpattern;
  1199. Consume(AS_ID);
  1200. MaybeRecordOffset;
  1201. if actasmtoken=AS_LBRACKET then
  1202. begin
  1203. if opr.typ<>OPR_REFERENCE then
  1204. begin
  1205. opr.typ:=OPR_REFERENCE;
  1206. reset_reference(opr.Ref);
  1207. end;
  1208. BuildReference;
  1209. end;
  1210. MaybeRecordOffset;
  1211. end;
  1212. end;
  1213. end;
  1214. AS_REGISTER : { Register, a variable reference or a constant reference }
  1215. Begin
  1216. { save the type of register used. }
  1217. tempreg:=actasmregister;
  1218. Consume(AS_REGISTER);
  1219. if actasmtoken = AS_COLON then
  1220. Begin
  1221. Consume(AS_COLON);
  1222. InitRef;
  1223. opr.ref.segment:=tempreg;
  1224. BuildReference;
  1225. end
  1226. else
  1227. { Simple register }
  1228. begin
  1229. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1230. Message(asmr_e_invalid_operand_type);
  1231. opr.typ:=OPR_REGISTER;
  1232. opr.reg:=tempreg;
  1233. size:=reg_2_opsize[opr.reg];
  1234. end;
  1235. end;
  1236. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1237. Begin
  1238. InitRef;
  1239. BuildReference;
  1240. MaybeRecordOffset;
  1241. end;
  1242. AS_SEG :
  1243. Begin
  1244. Message(asmr_e_seg_not_supported);
  1245. Consume(actasmtoken);
  1246. end;
  1247. AS_SEPARATOR,
  1248. AS_END,
  1249. AS_COMMA: ;
  1250. else
  1251. Message(asmr_e_syn_operand);
  1252. end;
  1253. if not(actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1254. begin
  1255. Message(asmr_e_syntax_error);
  1256. RecoverConsume(true);
  1257. end;
  1258. end;
  1259. {*****************************************************************************
  1260. T386IntelInstruction
  1261. *****************************************************************************}
  1262. type
  1263. P386IntelInstruction=^T386IntelInstruction;
  1264. T386IntelInstruction=object(T386Instruction)
  1265. procedure InitOperands;virtual;
  1266. procedure BuildOpcode;virtual;
  1267. end;
  1268. procedure T386IntelInstruction.InitOperands;
  1269. var
  1270. i : longint;
  1271. begin
  1272. for i:=1to 3 do
  1273. Operands[i]:=new(P386IntelOperand,Init);
  1274. end;
  1275. Procedure T386IntelInstruction.BuildOpCode;
  1276. var
  1277. PrefixOp,OverrideOp: tasmop;
  1278. expr : string;
  1279. size : topsize;
  1280. operandnum : longint;
  1281. Begin
  1282. expr:='';
  1283. PrefixOp:=A_None;
  1284. OverrideOp:=A_None;
  1285. { prefix seg opcode / prefix opcode }
  1286. repeat
  1287. if is_prefix(actopcode) then
  1288. begin
  1289. PrefixOp:=ActOpcode;
  1290. opcode:=ActOpcode;
  1291. condition:=ActCondition;
  1292. opsize:=ActOpsize;
  1293. ConcatInstruction(curlist);
  1294. Consume(AS_OPCODE);
  1295. end
  1296. else
  1297. if is_override(actopcode) then
  1298. begin
  1299. OverrideOp:=ActOpcode;
  1300. opcode:=ActOpcode;
  1301. condition:=ActCondition;
  1302. opsize:=ActOpsize;
  1303. ConcatInstruction(curlist);
  1304. Consume(AS_OPCODE);
  1305. end
  1306. else
  1307. break;
  1308. until (actasmtoken<>AS_OPCODE);
  1309. { opcode }
  1310. if (actasmtoken <> AS_OPCODE) then
  1311. Begin
  1312. Message(asmr_e_invalid_or_missing_opcode);
  1313. RecoverConsume(false);
  1314. exit;
  1315. end;
  1316. { Fill the instr object with the current state }
  1317. Opcode:=ActOpcode;
  1318. condition:=ActCondition;
  1319. opsize:=ActOpsize;
  1320. { Valid combination of prefix/override and instruction ? }
  1321. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1322. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1323. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1324. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1325. { We are reading operands, so opcode will be an AS_ID }
  1326. operandnum:=1;
  1327. Consume(AS_OPCODE);
  1328. { Zero operand opcode ? }
  1329. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1330. begin
  1331. operandnum:=0;
  1332. exit;
  1333. end;
  1334. { Read Operands }
  1335. repeat
  1336. case actasmtoken of
  1337. { End of asm operands for this opcode }
  1338. AS_END,
  1339. AS_SEPARATOR :
  1340. break;
  1341. { Operand delimiter }
  1342. AS_COMMA :
  1343. Begin
  1344. if operandnum > MaxOperands then
  1345. Message(asmr_e_too_many_operands)
  1346. else
  1347. Inc(operandnum);
  1348. Consume(AS_COMMA);
  1349. end;
  1350. { Typecast, Constant Expression, Type Specifier }
  1351. AS_DWORD,
  1352. AS_BYTE,
  1353. AS_WORD,
  1354. AS_TBYTE,
  1355. AS_QWORD :
  1356. Begin
  1357. { load the size in a temp variable, so it can be set when the
  1358. operand is read }
  1359. Case actasmtoken of
  1360. AS_DWORD : size:=S_L;
  1361. AS_WORD : size:=S_W;
  1362. AS_BYTE : size:=S_B;
  1363. AS_QWORD : size:=S_IQ;
  1364. AS_TBYTE : size:=S_FX;
  1365. end;
  1366. Consume(actasmtoken);
  1367. if actasmtoken=AS_PTR then
  1368. begin
  1369. Consume(AS_PTR);
  1370. Operands[operandnum]^.InitRef;
  1371. end;
  1372. Operands[operandnum]^.BuildOperand;
  1373. { now set the size which was specified by the override }
  1374. Operands[operandnum]^.size:=size;
  1375. end;
  1376. { Type specifier }
  1377. AS_NEAR,
  1378. AS_FAR :
  1379. Begin
  1380. if actasmtoken = AS_NEAR then
  1381. Message(asmr_w_near_ignored)
  1382. else
  1383. Message(asmr_w_far_ignored);
  1384. Consume(actasmtoken);
  1385. if actasmtoken=AS_PTR then
  1386. begin
  1387. Consume(AS_PTR);
  1388. Operands[operandnum]^.InitRef;
  1389. end;
  1390. Operands[operandnum]^.BuildOperand;
  1391. end;
  1392. else
  1393. Operands[operandnum]^.BuildOperand;
  1394. end; { end case }
  1395. until false;
  1396. Ops:=operandnum;
  1397. end;
  1398. Procedure BuildConstant(maxvalue: longint);
  1399. var
  1400. strlength: byte;
  1401. asmsym,
  1402. expr: string;
  1403. value : longint;
  1404. Begin
  1405. strlength:=0; { assume it is a DB }
  1406. Repeat
  1407. Case actasmtoken of
  1408. AS_STRING:
  1409. Begin
  1410. if maxvalue = $ffff then
  1411. strlength:=2
  1412. else
  1413. if maxvalue = $ffffffff then
  1414. strlength:=4;
  1415. { DD and DW cases }
  1416. if strlength <> 0 then
  1417. Begin
  1418. if Not PadZero(actasmpattern,strlength) then
  1419. Message(scan_f_string_exceeds_line);
  1420. end;
  1421. expr:=actasmpattern;
  1422. Consume(AS_STRING);
  1423. Case actasmtoken of
  1424. AS_COMMA:
  1425. Consume(AS_COMMA);
  1426. AS_END,
  1427. AS_SEPARATOR: ;
  1428. else
  1429. Message(asmr_e_invalid_string_expression);
  1430. end;
  1431. ConcatString(curlist,expr);
  1432. end;
  1433. AS_PLUS,
  1434. AS_MINUS,
  1435. AS_LPAREN,
  1436. AS_NOT,
  1437. AS_INTNUM,
  1438. AS_ID :
  1439. Begin
  1440. BuildConstSymbolExpression(false,false,value,asmsym);
  1441. if asmsym<>'' then
  1442. begin
  1443. if maxvalue<>$ffffffff then
  1444. Message(asmr_w_const32bit_for_address);
  1445. ConcatConstSymbol(curlist,asmsym,value)
  1446. end
  1447. else
  1448. ConcatConstant(curlist,value,maxvalue);
  1449. end;
  1450. AS_COMMA:
  1451. Consume(AS_COMMA);
  1452. AS_END,
  1453. AS_SEPARATOR:
  1454. break;
  1455. else
  1456. begin
  1457. Message(asmr_e_syn_constant);
  1458. RecoverConsume(false);
  1459. end
  1460. end;
  1461. Until false;
  1462. end;
  1463. Function Assemble: Ptree;
  1464. Var
  1465. hl : PAsmLabel;
  1466. instr : T386IntelInstruction;
  1467. Begin
  1468. Message1(asmr_d_start_reading,'intel');
  1469. inexpression:=FALSE;
  1470. firsttoken:=TRUE;
  1471. if assigned(procinfo.retdef) and
  1472. (is_fpu(procinfo.retdef) or
  1473. ret_in_acc(procinfo.retdef)) then
  1474. procinfo.funcret_is_valid:=true;
  1475. { sets up all opcode and register tables in uppercase }
  1476. if not _asmsorted then
  1477. Begin
  1478. SetupTables;
  1479. _asmsorted:=TRUE;
  1480. end;
  1481. curlist:=new(paasmoutput,init);
  1482. { setup label linked list }
  1483. new(LocalLabelList,Init);
  1484. { start tokenizer }
  1485. c:=current_scanner^.asmgetchar;
  1486. gettoken;
  1487. { main loop }
  1488. repeat
  1489. case actasmtoken of
  1490. AS_LLABEL:
  1491. Begin
  1492. if CreateLocalLabel(actasmpattern,hl,true) then
  1493. ConcatLabel(curlist,hl);
  1494. Consume(AS_LLABEL);
  1495. end;
  1496. AS_LABEL:
  1497. Begin
  1498. if SearchLabel(upper(actasmpattern),hl,true) then
  1499. ConcatLabel(curlist,hl)
  1500. else
  1501. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1502. Consume(AS_LABEL);
  1503. end;
  1504. AS_DW :
  1505. Begin
  1506. inexpression:=true;
  1507. Consume(AS_DW);
  1508. BuildConstant($ffff);
  1509. inexpression:=false;
  1510. end;
  1511. AS_DB :
  1512. Begin
  1513. inexpression:=true;
  1514. Consume(AS_DB);
  1515. BuildConstant($ff);
  1516. inexpression:=false;
  1517. end;
  1518. AS_DD :
  1519. Begin
  1520. inexpression:=true;
  1521. Consume(AS_DD);
  1522. BuildConstant($ffffffff);
  1523. inexpression:=false;
  1524. end;
  1525. AS_OPCODE :
  1526. Begin
  1527. instr.init;
  1528. instr.BuildOpcode;
  1529. { We need AT&T style operands }
  1530. instr.SwapOperands;
  1531. instr.AddReferenceSizes;
  1532. instr.SetInstructionOpsize;
  1533. instr.CheckOperandSizes;
  1534. instr.ConcatInstruction(curlist);
  1535. instr.done;
  1536. end;
  1537. AS_SEPARATOR :
  1538. Begin
  1539. Consume(AS_SEPARATOR);
  1540. end;
  1541. AS_END :
  1542. break; { end assembly block }
  1543. else
  1544. Begin
  1545. Message(asmr_e_syntax_error);
  1546. RecoverConsume(false);
  1547. end;
  1548. end; { end case }
  1549. until false;
  1550. { Check LocalLabelList }
  1551. LocalLabelList^.CheckEmitted;
  1552. dispose(LocalLabelList,Done);
  1553. { Return the list in an asmnode }
  1554. assemble:=genasmnode(curlist);
  1555. Message1(asmr_d_finish_reading,'intel');
  1556. end;
  1557. {*****************************************************************************
  1558. Initialize
  1559. *****************************************************************************}
  1560. var
  1561. old_exit : pointer;
  1562. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  1563. begin
  1564. if assigned(iasmops) then
  1565. dispose(iasmops);
  1566. if assigned(iasmregs) then
  1567. dispose(iasmregs);
  1568. exitproc:=old_exit;
  1569. end;
  1570. begin
  1571. old_exit:=exitproc;
  1572. exitproc:=@ra386int_exit;
  1573. end.
  1574. {
  1575. $Log$
  1576. Revision 1.43 1999-08-13 21:28:36 peter
  1577. * more reference types support
  1578. * arraydef size returns elementsize, also for multiple indexing array
  1579. Revision 1.42 1999/08/04 00:23:27 florian
  1580. * renamed i386asm and i386base to cpuasm and cpubase
  1581. Revision 1.41 1999/07/24 11:17:16 peter
  1582. * suffix parsing for at&t fixed for things like movsbl
  1583. * string constants are now handle correctly and also allowed in
  1584. constant expressions
  1585. Revision 1.40 1999/07/12 15:03:04 peter
  1586. * merged
  1587. Revision 1.39 1999/06/28 16:02:32 peter
  1588. * merged
  1589. Revision 1.37.2.4 1999/07/12 15:01:19 peter
  1590. * fixed @result parsing
  1591. Revision 1.37.2.3 1999/06/28 15:52:36 peter
  1592. * better recovery in main loop
  1593. Revision 1.37.2.2 1999/06/22 14:20:22 peter
  1594. * fixed parsing and writing of fpureg
  1595. Revision 1.37.2.1 1999/06/21 16:43:00 peter
  1596. * fixed label and opcode on the same line
  1597. Revision 1.37 1999/06/08 11:52:00 peter
  1598. * fixed some intel bugs with scale parsing
  1599. * end is now also a separator in many more cases
  1600. Revision 1.36 1999/06/01 19:56:37 peter
  1601. * fixed llabel with delete the first @
  1602. Revision 1.35 1999/05/27 19:44:59 peter
  1603. * removed oldasm
  1604. * plabel -> pasmlabel
  1605. * -a switches to source writing automaticly
  1606. * assembler readers OOPed
  1607. * asmsymbol automaticly external
  1608. * jumptables and other label fixes for asm readers
  1609. Revision 1.34 1999/05/21 13:55:16 peter
  1610. * NEWLAB for label as symbol
  1611. Revision 1.33 1999/05/05 22:22:03 peter
  1612. * updated messages
  1613. Revision 1.32 1999/05/04 21:45:02 florian
  1614. * changes to compile it with Delphi 4.0
  1615. Revision 1.31 1999/05/01 13:48:41 peter
  1616. * merged nasm compiler
  1617. Revision 1.6 1999/04/26 23:26:18 peter
  1618. * redesigned record offset parsing to support nested records
  1619. * normal compiler uses the redesigned createvarinstr()
  1620. Revision 1.5 1999/04/20 11:01:24 peter
  1621. * better tokenpos info
  1622. Revision 1.4 1999/04/14 09:07:46 peter
  1623. * asm reader improvements
  1624. Revision 1.3 1999/03/06 17:24:27 peter
  1625. * rewritten intel parser a lot, especially reference reading
  1626. * size checking added for asm parsers
  1627. Revision 1.2 1999/03/02 02:56:31 peter
  1628. + stabs support for binary writers
  1629. * more fixes and missing updates from the previous commit :(
  1630. Revision 1.1 1999/03/01 15:46:26 peter
  1631. * ag386bin finally make cycles correct
  1632. * prefixes are now also normal opcodes
  1633. }