ra386int.pas 49 KB

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