ra386int.pas 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. Unit Ra386int;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. cpubase,
  24. globtype,
  25. rasm,
  26. rax86;
  27. type
  28. tasmtoken = (
  29. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  30. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  31. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  32. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  33. {------------------ Assembler directives --------------------}
  34. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_END,
  35. {------------------ Assembler Operators --------------------}
  36. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_NEAR,AS_FAR,
  37. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  38. AS_AND,AS_OR,AS_XOR);
  39. type
  40. ti386intreader = class(tasmreader)
  41. actasmtoken : tasmtoken;
  42. prevasmtoken : tasmtoken;
  43. ActOpsize : topsize;
  44. constructor create;override;
  45. function is_asmopcode(const s: string):boolean;
  46. function is_asmoperator(const s: string):boolean;
  47. function is_asmdirective(const s: string):boolean;
  48. function is_register(const s:string):boolean;
  49. function is_locallabel(const s:string):boolean;
  50. function Assemble: tlinkedlist;override;
  51. procedure GetToken;
  52. function consume(t : tasmtoken):boolean;
  53. procedure RecoverConsume(allowcomma:boolean);
  54. procedure BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint);
  55. procedure BuildConstSymbolExpression(needofs,isref:boolean;var value:aint;var asmsym:string);
  56. function BuildConstExpression:aint;
  57. function BuildRefConstExpression:aint;
  58. procedure BuildReference(oper : tx86operand);
  59. procedure BuildOperand(oper: tx86operand);
  60. procedure BuildConstantOperand(oper: tx86operand);
  61. procedure BuildOpCode(instr : tx86instruction);
  62. procedure BuildConstant(constsize: longint);
  63. end;
  64. implementation
  65. uses
  66. { common }
  67. cutils,
  68. { global }
  69. globals,verbose,
  70. systems,
  71. { aasm }
  72. aasmbase,aasmtai,aasmcpu,
  73. { symtable }
  74. symconst,symbase,symtype,symsym,symdef,symtable,
  75. { parser }
  76. scanner,
  77. { register allocator }
  78. rabase,rautils,itx86int,
  79. { codegen }
  80. cgbase,cgobj,procinfo
  81. ;
  82. type
  83. tasmkeyword = string[6];
  84. const
  85. { These tokens should be modified accordingly to the modifications }
  86. { in the different enumerations. }
  87. firstdirective = AS_ALIGN;
  88. lastdirective = AS_END;
  89. firstoperator = AS_BYTE;
  90. lastoperator = AS_XOR;
  91. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  92. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  93. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  94. ('ALIGN','DB','DW','DD','END');
  95. { problems with shl,shr,not,and,or and xor, they are }
  96. { context sensitive. }
  97. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  98. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','NEAR','FAR','HIGH',
  99. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  100. 'OR','XOR');
  101. token2str : array[tasmtoken] of string[10] = (
  102. '','Label','LLabel','String','Integer',
  103. ',','[',']','(',
  104. ')',':','.','+','-','*',
  105. ';','identifier','register','opcode','/',
  106. '','','','','END',
  107. '','','','','','','','','',
  108. '','','','type','ptr','mod','shl','shr','not',
  109. 'and','or','xor'
  110. );
  111. var
  112. inexpression : boolean;
  113. constructor ti386intreader.create;
  114. var
  115. i : tasmop;
  116. str2opentry: tstr2opentry;
  117. Begin
  118. inherited create;
  119. { opcodes }
  120. { creates uppercased symbol tables for speed access }
  121. iasmops:=tdictionary.create;
  122. iasmops.delete_doubles:=true;
  123. for i:=firstop to lastop do
  124. begin
  125. str2opentry:=tstr2opentry.createname(upper(std_op2str[i]));
  126. str2opentry.op:=i;
  127. iasmops.insert(str2opentry);
  128. end;
  129. end;
  130. {---------------------------------------------------------------------}
  131. { Routines for the tokenizing }
  132. {---------------------------------------------------------------------}
  133. function ti386intreader.is_asmopcode(const s: string):boolean;
  134. var
  135. str2opentry: tstr2opentry;
  136. cond : string[4];
  137. cnd : tasmcond;
  138. j: longint;
  139. Begin
  140. is_asmopcode:=FALSE;
  141. actopcode:=A_None;
  142. actcondition:=C_None;
  143. actopsize:=S_NO;
  144. str2opentry:=tstr2opentry(iasmops.search(s));
  145. if assigned(str2opentry) then
  146. begin
  147. actopcode:=str2opentry.op;
  148. actasmtoken:=AS_OPCODE;
  149. is_asmopcode:=TRUE;
  150. exit;
  151. end;
  152. { not found yet, check condition opcodes }
  153. j:=0;
  154. while (j<CondAsmOps) do
  155. begin
  156. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  157. begin
  158. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  159. if cond<>'' then
  160. begin
  161. for cnd:=low(TasmCond) to high(TasmCond) do
  162. if Cond=Upper(cond2str[cnd]) then
  163. begin
  164. actopcode:=CondASmOp[j];
  165. actcondition:=cnd;
  166. is_asmopcode:=TRUE;
  167. actasmtoken:=AS_OPCODE;
  168. exit
  169. end;
  170. end;
  171. end;
  172. inc(j);
  173. end;
  174. end;
  175. function ti386intreader.is_asmoperator(const s: string):boolean;
  176. var
  177. i : longint;
  178. Begin
  179. for i:=0 to _count_asmoperators do
  180. if s=_asmoperators[i] then
  181. begin
  182. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  183. is_asmoperator:=true;
  184. exit;
  185. end;
  186. is_asmoperator:=false;
  187. end;
  188. Function ti386intreader.is_asmdirective(const s: string):boolean;
  189. var
  190. i : longint;
  191. Begin
  192. for i:=0 to _count_asmdirectives do
  193. if s=_asmdirectives[i] then
  194. begin
  195. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  196. is_asmdirective:=true;
  197. exit;
  198. end;
  199. is_asmdirective:=false;
  200. end;
  201. function ti386intreader.is_register(const s:string):boolean;
  202. begin
  203. is_register:=false;
  204. actasmregister:=masm_regnum_search(lower(s));
  205. if actasmregister<>NR_NO then
  206. begin
  207. is_register:=true;
  208. actasmtoken:=AS_REGISTER;
  209. end;
  210. end;
  211. function ti386intreader.is_locallabel(const s:string):boolean;
  212. begin
  213. is_locallabel:=(length(s)>1) and (s[1]='@');
  214. end;
  215. Procedure ti386intreader.GetToken;
  216. var
  217. len : longint;
  218. forcelabel : boolean;
  219. srsym : tsym;
  220. srsymtable : tsymtable;
  221. begin
  222. { save old token and reset new token }
  223. prevasmtoken:=actasmtoken;
  224. actasmtoken:=AS_NONE;
  225. { reset }
  226. forcelabel:=FALSE;
  227. actasmpattern:='';
  228. { while space and tab , continue scan... }
  229. while (c in [' ',#9]) do
  230. c:=current_scanner.asmgetchar;
  231. { get token pos }
  232. if not (c in [#10,#13,'{',';']) then
  233. current_scanner.gettokenpos;
  234. { Local Label, Label, Directive, Prefix or Opcode }
  235. if firsttoken and not (c in [#10,#13,'{',';']) then
  236. begin
  237. firsttoken:=FALSE;
  238. len:=0;
  239. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  240. begin
  241. { if there is an at_sign, then this must absolutely be a label }
  242. if c = '@' then
  243. forcelabel:=TRUE;
  244. inc(len);
  245. actasmpattern[len]:=c;
  246. c:=current_scanner.asmgetchar;
  247. end;
  248. actasmpattern[0]:=chr(len);
  249. uppervar(actasmpattern);
  250. { label ? }
  251. if c = ':' then
  252. begin
  253. if actasmpattern[1]='@' then
  254. actasmtoken:=AS_LLABEL
  255. else
  256. actasmtoken:=AS_LABEL;
  257. { let us point to the next character }
  258. c:=current_scanner.asmgetchar;
  259. firsttoken:=true;
  260. exit;
  261. end;
  262. { Are we trying to create an identifier with }
  263. { an at-sign...? }
  264. if forcelabel then
  265. Message(asmr_e_none_label_contain_at);
  266. { opcode ? }
  267. If is_asmopcode(actasmpattern) then
  268. Begin
  269. { check if we are in an expression }
  270. { then continue with asm directives }
  271. if not inexpression then
  272. exit;
  273. end;
  274. if is_asmdirective(actasmpattern) then
  275. exit;
  276. message1(asmr_e_unknown_opcode,actasmpattern);
  277. actasmtoken:=AS_NONE;
  278. exit;
  279. end
  280. else { else firsttoken }
  281. begin
  282. case c of
  283. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  284. { - @Result, @Code or @Data special variables. }
  285. begin
  286. actasmpattern:=c;
  287. c:=current_scanner.asmgetchar;
  288. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  289. begin
  290. actasmpattern:=actasmpattern + c;
  291. c:=current_scanner.asmgetchar;
  292. end;
  293. uppervar(actasmpattern);
  294. actasmtoken:=AS_ID;
  295. exit;
  296. end;
  297. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  298. begin
  299. actasmpattern:=c;
  300. c:=current_scanner.asmgetchar;
  301. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  302. begin
  303. actasmpattern:=actasmpattern + c;
  304. c:=current_scanner.asmgetchar;
  305. end;
  306. uppervar(actasmpattern);
  307. { after prefix we allow also a new opcode }
  308. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  309. Begin
  310. { if we are not in a constant }
  311. { expression than this is an }
  312. { opcode. }
  313. if not inexpression then
  314. exit;
  315. end;
  316. { support st(X) for fpu registers }
  317. if (actasmpattern = 'ST') and (c='(') then
  318. Begin
  319. actasmpattern:=actasmpattern+c;
  320. c:=current_scanner.asmgetchar;
  321. if c in ['0'..'7'] then
  322. actasmpattern:=actasmpattern + c
  323. else
  324. Message(asmr_e_invalid_fpu_register);
  325. c:=current_scanner.asmgetchar;
  326. if c <> ')' then
  327. Message(asmr_e_invalid_fpu_register)
  328. else
  329. Begin
  330. actasmpattern:=actasmpattern + c;
  331. c:=current_scanner.asmgetchar;
  332. end;
  333. end;
  334. if is_register(actasmpattern) then
  335. exit;
  336. if is_asmdirective(actasmpattern) then
  337. exit;
  338. if is_asmoperator(actasmpattern) then
  339. exit;
  340. { if next is a '.' and this is a unitsym then we also need to
  341. parse the identifier }
  342. if (c='.') then
  343. begin
  344. searchsym(actasmpattern,srsym,srsymtable);
  345. if assigned(srsym) and
  346. (srsym.typ=unitsym) and
  347. (srsym.owner.unitid=0) then
  348. begin
  349. { Add . to create System.Identifier }
  350. actasmpattern:=actasmpattern+c;
  351. c:=current_scanner.asmgetchar;
  352. { Delphi allows System.@Halt, just ignore the @ }
  353. if c='@' then
  354. c:=current_scanner.asmgetchar;
  355. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  356. begin
  357. actasmpattern:=actasmpattern + upcase(c);
  358. c:=current_scanner.asmgetchar;
  359. end;
  360. end;
  361. end;
  362. actasmtoken:=AS_ID;
  363. exit;
  364. end;
  365. '''' : { string or character }
  366. begin
  367. actasmpattern:='';
  368. current_scanner.in_asm_string:=true;
  369. repeat
  370. if c = '''' then
  371. begin
  372. c:=current_scanner.asmgetchar;
  373. if c in [#10,#13] then
  374. begin
  375. Message(scan_f_string_exceeds_line);
  376. break;
  377. end;
  378. repeat
  379. if c='''' then
  380. begin
  381. c:=current_scanner.asmgetchar;
  382. if c='''' then
  383. begin
  384. actasmpattern:=actasmpattern+'''';
  385. c:=current_scanner.asmgetchar;
  386. if c in [#10,#13] then
  387. begin
  388. Message(scan_f_string_exceeds_line);
  389. break;
  390. end;
  391. end
  392. else
  393. break;
  394. end
  395. else
  396. begin
  397. actasmpattern:=actasmpattern+c;
  398. c:=current_scanner.asmgetchar;
  399. if c in [#10,#13] then
  400. begin
  401. Message(scan_f_string_exceeds_line);
  402. break
  403. end;
  404. end;
  405. until false; { end repeat }
  406. end
  407. else
  408. break; { end if }
  409. until false;
  410. current_scanner.in_asm_string:=false;
  411. actasmtoken:=AS_STRING;
  412. exit;
  413. end;
  414. '"' : { string or character }
  415. begin
  416. current_scanner.in_asm_string:=true;
  417. actasmpattern:='';
  418. repeat
  419. if c = '"' then
  420. begin
  421. c:=current_scanner.asmgetchar;
  422. if c in [#10,#13] then
  423. begin
  424. Message(scan_f_string_exceeds_line);
  425. break;
  426. end;
  427. repeat
  428. if c='"' then
  429. begin
  430. c:=current_scanner.asmgetchar;
  431. if c='"' then
  432. begin
  433. actasmpattern:=actasmpattern+'"';
  434. c:=current_scanner.asmgetchar;
  435. if c in [#10,#13] then
  436. begin
  437. Message(scan_f_string_exceeds_line);
  438. break;
  439. end;
  440. end
  441. else
  442. break;
  443. end
  444. else
  445. begin
  446. actasmpattern:=actasmpattern+c;
  447. c:=current_scanner.asmgetchar;
  448. if c in [#10,#13] then
  449. begin
  450. Message(scan_f_string_exceeds_line);
  451. break
  452. end;
  453. end;
  454. until false; { end repeat }
  455. end
  456. else
  457. break; { end if }
  458. until false;
  459. current_scanner.in_asm_string:=false;
  460. actasmtoken:=AS_STRING;
  461. exit;
  462. end;
  463. '$' :
  464. begin
  465. c:=current_scanner.asmgetchar;
  466. while c in ['0'..'9','A'..'F','a'..'f'] do
  467. begin
  468. actasmpattern:=actasmpattern + c;
  469. c:=current_scanner.asmgetchar;
  470. end;
  471. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  472. actasmtoken:=AS_INTNUM;
  473. exit;
  474. end;
  475. ',' :
  476. begin
  477. actasmtoken:=AS_COMMA;
  478. c:=current_scanner.asmgetchar;
  479. exit;
  480. end;
  481. '[' :
  482. begin
  483. actasmtoken:=AS_LBRACKET;
  484. c:=current_scanner.asmgetchar;
  485. exit;
  486. end;
  487. ']' :
  488. begin
  489. actasmtoken:=AS_RBRACKET;
  490. c:=current_scanner.asmgetchar;
  491. exit;
  492. end;
  493. '(' :
  494. begin
  495. actasmtoken:=AS_LPAREN;
  496. c:=current_scanner.asmgetchar;
  497. exit;
  498. end;
  499. ')' :
  500. begin
  501. actasmtoken:=AS_RPAREN;
  502. c:=current_scanner.asmgetchar;
  503. exit;
  504. end;
  505. ':' :
  506. begin
  507. actasmtoken:=AS_COLON;
  508. c:=current_scanner.asmgetchar;
  509. exit;
  510. end;
  511. '.' :
  512. begin
  513. actasmtoken:=AS_DOT;
  514. c:=current_scanner.asmgetchar;
  515. exit;
  516. end;
  517. '+' :
  518. begin
  519. actasmtoken:=AS_PLUS;
  520. c:=current_scanner.asmgetchar;
  521. exit;
  522. end;
  523. '-' :
  524. begin
  525. actasmtoken:=AS_MINUS;
  526. c:=current_scanner.asmgetchar;
  527. exit;
  528. end;
  529. '*' :
  530. begin
  531. actasmtoken:=AS_STAR;
  532. c:=current_scanner.asmgetchar;
  533. exit;
  534. end;
  535. '/' :
  536. begin
  537. actasmtoken:=AS_SLASH;
  538. c:=current_scanner.asmgetchar;
  539. exit;
  540. end;
  541. '0'..'9':
  542. begin
  543. actasmpattern:=c;
  544. c:=current_scanner.asmgetchar;
  545. { Get the possible characters }
  546. while c in ['0'..'9','A'..'F','a'..'f'] do
  547. begin
  548. actasmpattern:=actasmpattern + c;
  549. c:=current_scanner.asmgetchar;
  550. end;
  551. { Get ending character }
  552. uppervar(actasmpattern);
  553. c:=upcase(c);
  554. { possibly a binary number. }
  555. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  556. Begin
  557. { Delete the last binary specifier }
  558. delete(actasmpattern,length(actasmpattern),1);
  559. actasmpattern:=tostr(ValBinary(actasmpattern));
  560. actasmtoken:=AS_INTNUM;
  561. exit;
  562. end
  563. else
  564. Begin
  565. case c of
  566. 'O' :
  567. Begin
  568. actasmpattern:=tostr(ValOctal(actasmpattern));
  569. actasmtoken:=AS_INTNUM;
  570. c:=current_scanner.asmgetchar;
  571. exit;
  572. end;
  573. 'H' :
  574. Begin
  575. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  576. actasmtoken:=AS_INTNUM;
  577. c:=current_scanner.asmgetchar;
  578. exit;
  579. end;
  580. else { must be an integer number }
  581. begin
  582. actasmpattern:=tostr(ValDecimal(actasmpattern));
  583. actasmtoken:=AS_INTNUM;
  584. exit;
  585. end;
  586. end;
  587. end;
  588. end;
  589. ';','{',#13,#10 :
  590. begin
  591. c:=current_scanner.asmgetchar;
  592. firsttoken:=TRUE;
  593. actasmtoken:=AS_SEPARATOR;
  594. exit;
  595. end;
  596. else
  597. current_scanner.illegal_char(c);
  598. end;
  599. end;
  600. end;
  601. function ti386intreader.consume(t : tasmtoken):boolean;
  602. begin
  603. Consume:=true;
  604. if t<>actasmtoken then
  605. begin
  606. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  607. Consume:=false;
  608. end;
  609. repeat
  610. gettoken;
  611. until actasmtoken<>AS_NONE;
  612. end;
  613. procedure ti386intreader.RecoverConsume(allowcomma:boolean);
  614. begin
  615. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  616. begin
  617. if allowcomma and (actasmtoken=AS_COMMA) then
  618. break;
  619. Consume(actasmtoken);
  620. end;
  621. end;
  622. {*****************************************************************************
  623. Parsing Helpers
  624. *****************************************************************************}
  625. { This routine builds up a record offset after a AS_DOT
  626. token is encountered.
  627. On entry actasmtoken should be equal to AS_DOT }
  628. Procedure ti386intreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint);
  629. var
  630. s : string;
  631. Begin
  632. offset:=0;
  633. size:=0;
  634. s:=expr;
  635. while (actasmtoken=AS_DOT) do
  636. begin
  637. Consume(AS_DOT);
  638. if actasmtoken=AS_ID then
  639. s:=s+'.'+actasmpattern;
  640. if not Consume(AS_ID) then
  641. begin
  642. RecoverConsume(true);
  643. break;
  644. end;
  645. end;
  646. if not GetRecordOffsetSize(s,offset,size) then
  647. Message(asmr_e_building_record_offset);
  648. end;
  649. Procedure ti386intreader.BuildConstSymbolExpression(needofs,isref:boolean;var value:aint;var asmsym:string);
  650. var
  651. tempstr,expr,hs : string;
  652. parenlevel : longint;
  653. l,k : aint;
  654. hasparen,
  655. errorflag : boolean;
  656. prevtok : tasmtoken;
  657. hl : tasmlabel;
  658. def : tdef;
  659. sym : tsym;
  660. srsymtable : tsymtable;
  661. Begin
  662. { reset }
  663. value:=0;
  664. asmsym:='';
  665. errorflag:=FALSE;
  666. tempstr:='';
  667. expr:='';
  668. inexpression:=TRUE;
  669. parenlevel:=0;
  670. Repeat
  671. Case actasmtoken of
  672. AS_LPAREN:
  673. Begin
  674. Consume(AS_LPAREN);
  675. expr:=expr + '(';
  676. inc(parenlevel);
  677. end;
  678. AS_RPAREN:
  679. Begin
  680. Consume(AS_RPAREN);
  681. expr:=expr + ')';
  682. dec(parenlevel);
  683. end;
  684. AS_SHL:
  685. Begin
  686. Consume(AS_SHL);
  687. expr:=expr + '<';
  688. end;
  689. AS_SHR:
  690. Begin
  691. Consume(AS_SHR);
  692. expr:=expr + '>';
  693. end;
  694. AS_SLASH:
  695. Begin
  696. Consume(AS_SLASH);
  697. expr:=expr + '/';
  698. end;
  699. AS_MOD:
  700. Begin
  701. Consume(AS_MOD);
  702. expr:=expr + '%';
  703. end;
  704. AS_STAR:
  705. Begin
  706. Consume(AS_STAR);
  707. if isref and (actasmtoken=AS_REGISTER) then
  708. break;
  709. expr:=expr + '*';
  710. end;
  711. AS_PLUS:
  712. Begin
  713. Consume(AS_PLUS);
  714. if isref and (actasmtoken=AS_REGISTER) then
  715. break;
  716. expr:=expr + '+';
  717. end;
  718. AS_LBRACKET:
  719. begin
  720. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  721. if isref then
  722. break;
  723. end;
  724. AS_MINUS:
  725. Begin
  726. Consume(AS_MINUS);
  727. expr:=expr + '-';
  728. end;
  729. AS_AND:
  730. Begin
  731. Consume(AS_AND);
  732. expr:=expr + '&';
  733. end;
  734. AS_NOT:
  735. Begin
  736. Consume(AS_NOT);
  737. expr:=expr + '~';
  738. end;
  739. AS_XOR:
  740. Begin
  741. Consume(AS_XOR);
  742. expr:=expr + '^';
  743. end;
  744. AS_OR:
  745. Begin
  746. Consume(AS_OR);
  747. expr:=expr + '|';
  748. end;
  749. AS_INTNUM:
  750. Begin
  751. expr:=expr + actasmpattern;
  752. Consume(AS_INTNUM);
  753. end;
  754. AS_OFFSET:
  755. begin
  756. Consume(AS_OFFSET);
  757. needofs:=true;
  758. if actasmtoken<>AS_ID then
  759. Message(asmr_e_offset_without_identifier);
  760. end;
  761. AS_TYPE:
  762. begin
  763. l:=0;
  764. hasparen:=false;
  765. Consume(AS_TYPE);
  766. if actasmtoken=AS_LPAREN then
  767. begin
  768. hasparen:=true;
  769. Consume(AS_LPAREN);
  770. end;
  771. if actasmtoken<>AS_ID then
  772. Message(asmr_e_type_without_identifier)
  773. else
  774. begin
  775. tempstr:=actasmpattern;
  776. Consume(AS_ID);
  777. if actasmtoken=AS_DOT then
  778. BuildRecordOffsetSize(tempstr,k,l)
  779. else
  780. begin
  781. searchsym(tempstr,sym,srsymtable);
  782. if assigned(sym) then
  783. begin
  784. case sym.typ of
  785. globalvarsym,
  786. localvarsym,
  787. paravarsym :
  788. l:=tabstractvarsym(sym).getsize;
  789. typedconstsym :
  790. l:=ttypedconstsym(sym).getsize;
  791. typesym :
  792. l:=ttypesym(sym).restype.def.size;
  793. else
  794. Message(asmr_e_wrong_sym_type);
  795. end;
  796. end
  797. else
  798. Message1(sym_e_unknown_id,tempstr);
  799. end;
  800. end;
  801. str(l, tempstr);
  802. expr:=expr + tempstr;
  803. if hasparen then
  804. Consume(AS_RPAREN);
  805. end;
  806. AS_STRING:
  807. Begin
  808. l:=0;
  809. case Length(actasmpattern) of
  810. 1 :
  811. l:=ord(actasmpattern[1]);
  812. 2 :
  813. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  814. 3 :
  815. l:=ord(actasmpattern[3]) +
  816. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  817. 4 :
  818. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  819. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  820. else
  821. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  822. end;
  823. str(l, tempstr);
  824. expr:=expr + tempstr;
  825. Consume(AS_STRING);
  826. end;
  827. AS_ID:
  828. Begin
  829. hs:='';
  830. def:=nil;
  831. tempstr:=actasmpattern;
  832. prevtok:=prevasmtoken;
  833. consume(AS_ID);
  834. if SearchIConstant(tempstr,l) then
  835. begin
  836. str(l, tempstr);
  837. expr:=expr + tempstr;
  838. end
  839. else
  840. begin
  841. if is_locallabel(tempstr) then
  842. begin
  843. CreateLocalLabel(tempstr,hl,false);
  844. hs:=hl.name
  845. end
  846. else
  847. if SearchLabel(tempstr,hl,false) then
  848. hs:=hl.name
  849. else
  850. begin
  851. searchsym(tempstr,sym,srsymtable);
  852. if assigned(sym) then
  853. begin
  854. case sym.typ of
  855. globalvarsym :
  856. begin
  857. hs:=tglobalvarsym(sym).mangledname;
  858. def:=tglobalvarsym(sym).vartype.def;
  859. end;
  860. localvarsym,
  861. paravarsym :
  862. begin
  863. Message(asmr_e_no_local_or_para_allowed);
  864. end;
  865. typedconstsym :
  866. begin
  867. hs:=ttypedconstsym(sym).mangledname;
  868. def:=ttypedconstsym(sym).typedconsttype.def;
  869. end;
  870. procsym :
  871. begin
  872. if Tprocsym(sym).procdef_count>1 then
  873. Message(asmr_w_calling_overload_func);
  874. hs:=tprocsym(sym).first_procdef.mangledname;
  875. end;
  876. typesym :
  877. begin
  878. if not(ttypesym(sym).restype.def.deftype in [recorddef,objectdef]) then
  879. Message(asmr_e_wrong_sym_type);
  880. end;
  881. else
  882. Message(asmr_e_wrong_sym_type);
  883. end;
  884. end
  885. else
  886. Message1(sym_e_unknown_id,tempstr);
  887. end;
  888. { symbol found? }
  889. if hs<>'' then
  890. begin
  891. if asmsym='' then
  892. asmsym:=hs
  893. else
  894. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  895. if (expr='') or (expr[length(expr)]='+') then
  896. begin
  897. { don't remove the + if there could be a record field }
  898. if actasmtoken<>AS_DOT then
  899. delete(expr,length(expr),1);
  900. end
  901. else
  902. if needofs then
  903. begin
  904. if (prevtok<>AS_OFFSET) then
  905. Message(asmr_e_need_offset);
  906. end
  907. else
  908. Message(asmr_e_only_add_relocatable_symbol);
  909. end;
  910. if actasmtoken=AS_DOT then
  911. begin
  912. BuildRecordOffsetSize(tempstr,l,k);
  913. str(l, tempstr);
  914. expr:=expr + tempstr;
  915. end
  916. else
  917. begin
  918. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  919. delete(expr,length(expr),1);
  920. end;
  921. if (actasmtoken=AS_LBRACKET) and
  922. assigned(def) and
  923. (def.deftype=arraydef) then
  924. begin
  925. consume(AS_LBRACKET);
  926. l:=BuildConstExpression;
  927. if l<tarraydef(def).lowrange then
  928. begin
  929. Message(asmr_e_constant_out_of_bounds);
  930. l:=0;
  931. end
  932. else
  933. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  934. str(l, tempstr);
  935. expr:=expr + '+' + tempstr;
  936. consume(AS_RBRACKET);
  937. end;
  938. end;
  939. { check if there are wrong operator used like / or mod etc. }
  940. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  941. Message(asmr_e_only_add_relocatable_symbol);
  942. end;
  943. AS_END,
  944. AS_RBRACKET,
  945. AS_SEPARATOR,
  946. AS_COMMA:
  947. Begin
  948. break;
  949. end;
  950. else
  951. Begin
  952. { write error only once. }
  953. if not errorflag then
  954. Message(asmr_e_invalid_constant_expression);
  955. { consume tokens until we find COMMA or SEPARATOR }
  956. Consume(actasmtoken);
  957. errorflag:=TRUE;
  958. end;
  959. end;
  960. Until false;
  961. { calculate expression }
  962. if not ErrorFlag then
  963. value:=CalculateExpression(expr)
  964. else
  965. value:=0;
  966. { no longer in an expression }
  967. inexpression:=FALSE;
  968. end;
  969. Function ti386intreader.BuildConstExpression:aint;
  970. var
  971. l : aint;
  972. hs : string;
  973. begin
  974. BuildConstSymbolExpression(false,false,l,hs);
  975. if hs<>'' then
  976. Message(asmr_e_relocatable_symbol_not_allowed);
  977. BuildConstExpression:=l;
  978. end;
  979. Function ti386intreader.BuildRefConstExpression:aint;
  980. var
  981. l : aint;
  982. hs : string;
  983. begin
  984. BuildConstSymbolExpression(false,true,l,hs);
  985. if hs<>'' then
  986. Message(asmr_e_relocatable_symbol_not_allowed);
  987. BuildRefConstExpression:=l;
  988. end;
  989. procedure ti386intreader.BuildReference(oper : tx86operand);
  990. var
  991. k,l,scale : aint;
  992. tempstr,hs : string;
  993. typesize : longint;
  994. code : integer;
  995. hreg : tregister;
  996. GotStar,GotOffset,HadVar,
  997. GotPlus,Negative : boolean;
  998. Begin
  999. Consume(AS_LBRACKET);
  1000. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1001. oper.InitRef;
  1002. GotStar:=false;
  1003. GotPlus:=true;
  1004. GotOffset:=false;
  1005. Negative:=false;
  1006. Scale:=0;
  1007. repeat
  1008. if GotOffset and (actasmtoken<>AS_ID) then
  1009. Message(asmr_e_invalid_reference_syntax);
  1010. Case actasmtoken of
  1011. AS_ID: { Constant reference expression OR variable reference expression }
  1012. Begin
  1013. if not GotPlus then
  1014. Message(asmr_e_invalid_reference_syntax);
  1015. if actasmpattern[1] = '@' then
  1016. Message(asmr_e_local_label_not_allowed_as_ref);
  1017. GotStar:=false;
  1018. GotPlus:=false;
  1019. if SearchIConstant(actasmpattern,l) or
  1020. SearchRecordType(actasmpattern) then
  1021. begin
  1022. l:=BuildRefConstExpression;
  1023. GotPlus:=(prevasmtoken=AS_PLUS);
  1024. GotStar:=(prevasmtoken=AS_STAR);
  1025. case oper.opr.typ of
  1026. OPR_LOCAL :
  1027. begin
  1028. if GotStar then
  1029. Message(asmr_e_invalid_reference_syntax);
  1030. if negative then
  1031. Dec(oper.opr.localsymofs,l)
  1032. else
  1033. Inc(oper.opr.localsymofs,l);
  1034. end;
  1035. OPR_REFERENCE :
  1036. begin
  1037. if GotStar then
  1038. oper.opr.ref.scalefactor:=l
  1039. else
  1040. begin
  1041. if negative then
  1042. Dec(oper.opr.ref.offset,l)
  1043. else
  1044. Inc(oper.opr.ref.offset,l);
  1045. end;
  1046. end;
  1047. end;
  1048. end
  1049. else
  1050. Begin
  1051. if oper.hasvar and not GotOffset then
  1052. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1053. HadVar:=oper.hasvar and GotOffset;
  1054. if negative then
  1055. Message(asmr_e_only_add_relocatable_symbol);
  1056. tempstr:=actasmpattern;
  1057. Consume(AS_ID);
  1058. { typecasting? }
  1059. if (actasmtoken=AS_LPAREN) and
  1060. SearchType(tempstr,typesize) then
  1061. begin
  1062. oper.hastype:=true;
  1063. Consume(AS_LPAREN);
  1064. BuildOperand(oper);
  1065. Consume(AS_RPAREN);
  1066. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  1067. oper.SetSize(typesize,true);
  1068. end
  1069. else
  1070. if not oper.SetupVar(tempstr,GotOffset) then
  1071. Message1(sym_e_unknown_id,tempstr);
  1072. { record.field ? }
  1073. if actasmtoken=AS_DOT then
  1074. begin
  1075. BuildRecordOffsetSize(tempstr,l,k);
  1076. case oper.opr.typ of
  1077. OPR_LOCAL :
  1078. inc(oper.opr.localsymofs,l);
  1079. OPR_REFERENCE :
  1080. inc(oper.opr.ref.offset,l);
  1081. end;
  1082. end;
  1083. if GotOffset then
  1084. begin
  1085. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1086. begin
  1087. if (oper.opr.typ=OPR_REFERENCE) then
  1088. oper.opr.ref.base:=NR_NO;
  1089. oper.hasvar:=hadvar;
  1090. end
  1091. else
  1092. begin
  1093. if oper.hasvar and hadvar then
  1094. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1095. { should we allow ?? }
  1096. end;
  1097. end;
  1098. end;
  1099. GotOffset:=false;
  1100. end;
  1101. AS_PLUS :
  1102. Begin
  1103. Consume(AS_PLUS);
  1104. Negative:=false;
  1105. GotPlus:=true;
  1106. GotStar:=false;
  1107. Scale:=0;
  1108. end;
  1109. AS_MINUS :
  1110. begin
  1111. Consume(AS_MINUS);
  1112. Negative:=true;
  1113. GotPlus:=true;
  1114. GotStar:=false;
  1115. Scale:=0;
  1116. end;
  1117. AS_STAR : { Scaling, with eax*4 order }
  1118. begin
  1119. Consume(AS_STAR);
  1120. hs:='';
  1121. l:=0;
  1122. case actasmtoken of
  1123. AS_LPAREN :
  1124. l:=BuildConstExpression;
  1125. AS_INTNUM:
  1126. Begin
  1127. hs:=actasmpattern;
  1128. Consume(AS_INTNUM);
  1129. end;
  1130. AS_REGISTER :
  1131. begin
  1132. case oper.opr.typ of
  1133. OPR_REFERENCE :
  1134. begin
  1135. if oper.opr.ref.scalefactor=0 then
  1136. begin
  1137. if scale<>0 then
  1138. begin
  1139. oper.opr.ref.scalefactor:=scale;
  1140. scale:=0;
  1141. end
  1142. else
  1143. Message(asmr_e_wrong_scale_factor);
  1144. end
  1145. else
  1146. Message(asmr_e_invalid_reference_syntax);
  1147. end;
  1148. OPR_LOCAL :
  1149. begin
  1150. if oper.opr.localscale=0 then
  1151. begin
  1152. if scale<>0 then
  1153. begin
  1154. oper.opr.localscale:=scale;
  1155. scale:=0;
  1156. end
  1157. else
  1158. Message(asmr_e_wrong_scale_factor);
  1159. end
  1160. else
  1161. Message(asmr_e_invalid_reference_syntax);
  1162. end;
  1163. end;
  1164. end;
  1165. else
  1166. Message(asmr_e_invalid_reference_syntax);
  1167. end;
  1168. if actasmtoken<>AS_REGISTER then
  1169. begin
  1170. if hs<>'' then
  1171. val(hs,l,code);
  1172. case oper.opr.typ of
  1173. OPR_REFERENCE :
  1174. oper.opr.ref.scalefactor:=l;
  1175. OPR_LOCAL :
  1176. oper.opr.localscale:=l;
  1177. end;
  1178. if l>9 then
  1179. Message(asmr_e_wrong_scale_factor);
  1180. end;
  1181. GotPlus:=false;
  1182. GotStar:=false;
  1183. end;
  1184. AS_REGISTER :
  1185. begin
  1186. if not((GotPlus and (not Negative)) or
  1187. GotStar) then
  1188. Message(asmr_e_invalid_reference_syntax);
  1189. hreg:=actasmregister;
  1190. Consume(AS_REGISTER);
  1191. { this register will be the index:
  1192. 1. just read a *
  1193. 2. next token is a *
  1194. 3. base register is already used }
  1195. case oper.opr.typ of
  1196. OPR_LOCAL :
  1197. begin
  1198. if (oper.opr.localindexreg<>NR_NO) then
  1199. Message(asmr_e_multiple_index);
  1200. oper.opr.localindexreg:=hreg;
  1201. if scale<>0 then
  1202. begin
  1203. oper.opr.localscale:=scale;
  1204. scale:=0;
  1205. end;
  1206. end;
  1207. OPR_REFERENCE :
  1208. begin
  1209. if (GotStar) or
  1210. (actasmtoken=AS_STAR) or
  1211. (oper.opr.ref.base<>NR_NO) then
  1212. begin
  1213. if (oper.opr.ref.index<>NR_NO) then
  1214. Message(asmr_e_multiple_index);
  1215. oper.opr.ref.index:=hreg;
  1216. if scale<>0 then
  1217. begin
  1218. oper.opr.ref.scalefactor:=scale;
  1219. scale:=0;
  1220. end;
  1221. end
  1222. else
  1223. oper.opr.ref.base:=hreg;
  1224. end;
  1225. end;
  1226. GotPlus:=false;
  1227. GotStar:=false;
  1228. end;
  1229. AS_OFFSET :
  1230. begin
  1231. Consume(AS_OFFSET);
  1232. GotOffset:=true;
  1233. end;
  1234. AS_TYPE,
  1235. AS_NOT,
  1236. AS_STRING,
  1237. AS_INTNUM,
  1238. AS_LPAREN : { Constant reference expression }
  1239. begin
  1240. if not GotPlus and not GotStar then
  1241. Message(asmr_e_invalid_reference_syntax);
  1242. BuildConstSymbolExpression(true,true,l,tempstr);
  1243. if tempstr<>'' then
  1244. begin
  1245. if GotStar then
  1246. Message(asmr_e_only_add_relocatable_symbol);
  1247. if not assigned(oper.opr.ref.symbol) then
  1248. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,AT_FUNCTION)
  1249. else
  1250. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1251. end;
  1252. case oper.opr.typ of
  1253. OPR_REFERENCE :
  1254. begin
  1255. if GotStar then
  1256. oper.opr.ref.scalefactor:=l
  1257. else if (prevasmtoken = AS_STAR) then
  1258. begin
  1259. if scale<>0 then
  1260. scale:=l*scale
  1261. else
  1262. scale:=l;
  1263. end
  1264. else
  1265. begin
  1266. if negative then
  1267. Dec(oper.opr.ref.offset,l)
  1268. else
  1269. Inc(oper.opr.ref.offset,l);
  1270. end;
  1271. end;
  1272. OPR_LOCAL :
  1273. begin
  1274. if GotStar then
  1275. oper.opr.localscale:=l
  1276. else if (prevasmtoken = AS_STAR) then
  1277. begin
  1278. if scale<>0 then
  1279. scale:=l*scale
  1280. else
  1281. scale:=l;
  1282. end
  1283. else
  1284. begin
  1285. if negative then
  1286. Dec(oper.opr.localsymofs,l)
  1287. else
  1288. Inc(oper.opr.localsymofs,l);
  1289. end;
  1290. end;
  1291. end;
  1292. GotPlus:=(prevasmtoken=AS_PLUS) or
  1293. (prevasmtoken=AS_MINUS);
  1294. if GotPlus then
  1295. negative := prevasmtoken = AS_MINUS;
  1296. GotStar:=(prevasmtoken=AS_STAR);
  1297. end;
  1298. AS_RBRACKET :
  1299. begin
  1300. if GotPlus or GotStar then
  1301. Message(asmr_e_invalid_reference_syntax);
  1302. Consume(AS_RBRACKET);
  1303. break;
  1304. end;
  1305. else
  1306. Begin
  1307. Message(asmr_e_invalid_reference_syntax);
  1308. RecoverConsume(true);
  1309. break;
  1310. end;
  1311. end;
  1312. until false;
  1313. end;
  1314. Procedure ti386intreader.BuildConstantOperand(oper: tx86operand);
  1315. var
  1316. l : aint;
  1317. tempstr : string;
  1318. begin
  1319. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1320. Message(asmr_e_invalid_operand_type);
  1321. BuildConstSymbolExpression(true,false,l,tempstr);
  1322. if tempstr<>'' then
  1323. begin
  1324. oper.opr.typ:=OPR_SYMBOL;
  1325. oper.opr.symofs:=l;
  1326. { the symbol already exists, but we don't know whether it is data or
  1327. a function. we can use AT_NONE }
  1328. oper.opr.symbol:=objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,AT_NONE);
  1329. end
  1330. else
  1331. begin
  1332. if oper.opr.typ=OPR_NONE then
  1333. begin
  1334. oper.opr.typ:=OPR_CONSTANT;
  1335. oper.opr.val:=l;
  1336. end
  1337. else
  1338. inc(oper.opr.val,l);
  1339. end;
  1340. end;
  1341. Procedure ti386intreader.BuildOperand(oper: tx86operand);
  1342. procedure AddLabelOperand(hl:tasmlabel);
  1343. begin
  1344. if is_calljmp(actopcode) then
  1345. begin
  1346. oper.opr.typ:=OPR_SYMBOL;
  1347. oper.opr.symbol:=hl;
  1348. end
  1349. else
  1350. begin
  1351. oper.InitRef;
  1352. oper.opr.ref.symbol:=hl;
  1353. end;
  1354. end;
  1355. var
  1356. expr : string;
  1357. tempreg : tregister;
  1358. typesize,
  1359. l : aint;
  1360. hl : tasmlabel;
  1361. toffset,
  1362. tsize : aint;
  1363. Begin
  1364. expr:='';
  1365. repeat
  1366. if actasmtoken=AS_DOT then
  1367. begin
  1368. if expr<>'' then
  1369. begin
  1370. BuildRecordOffsetSize(expr,toffset,tsize);
  1371. oper.SetSize(tsize,true);
  1372. case oper.opr.typ of
  1373. OPR_LOCAL :
  1374. begin
  1375. { don't allow direct access to fields of parameters, becuase that
  1376. will generate buggy code. Allow it only for explicit typecasting
  1377. and when the parameter is in a register (delphi compatible) }
  1378. if (not oper.hastype) and
  1379. (oper.opr.localsym.owner.symtabletype=parasymtable) and
  1380. (current_procinfo.procdef.proccalloption<>pocall_register) then
  1381. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1382. inc(oper.opr.localsymofs,toffset)
  1383. end;
  1384. OPR_CONSTANT :
  1385. inc(oper.opr.val,toffset);
  1386. OPR_REFERENCE :
  1387. inc(oper.opr.ref.offset,toffset);
  1388. OPR_NONE :
  1389. begin
  1390. oper.opr.typ:=OPR_CONSTANT;
  1391. oper.opr.val:=toffset;
  1392. end;
  1393. else
  1394. internalerror(200309222);
  1395. end;
  1396. expr:='';
  1397. end
  1398. else
  1399. begin
  1400. { See it as a separator }
  1401. Consume(AS_DOT);
  1402. end;
  1403. end;
  1404. case actasmtoken of
  1405. AS_OFFSET,
  1406. AS_TYPE,
  1407. AS_NOT,
  1408. AS_STRING :
  1409. Begin
  1410. BuildConstantOperand(oper);
  1411. end;
  1412. AS_PLUS,
  1413. AS_MINUS,
  1414. AS_LPAREN,
  1415. AS_INTNUM :
  1416. begin
  1417. case oper.opr.typ of
  1418. OPR_REFERENCE :
  1419. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1420. OPR_LOCAL :
  1421. inc(oper.opr.localsymofs,BuildConstExpression);
  1422. OPR_NONE,
  1423. OPR_CONSTANT :
  1424. BuildConstantOperand(oper);
  1425. else
  1426. Message(asmr_e_invalid_operand_type);
  1427. end;
  1428. end;
  1429. AS_ID : { A constant expression, or a Variable ref. }
  1430. Begin
  1431. { Label or Special symbol reference? }
  1432. if actasmpattern[1] = '@' then
  1433. Begin
  1434. if actasmpattern = '@RESULT' then
  1435. Begin
  1436. oper.SetupResult;
  1437. Consume(AS_ID);
  1438. end
  1439. else
  1440. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1441. begin
  1442. Message(asmr_w_CODE_and_DATA_not_supported);
  1443. Consume(AS_ID);
  1444. end
  1445. else
  1446. { Local Label }
  1447. begin
  1448. CreateLocalLabel(actasmpattern,hl,false);
  1449. Consume(AS_ID);
  1450. AddLabelOperand(hl);
  1451. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1452. Message(asmr_e_syntax_error);
  1453. end;
  1454. end
  1455. else
  1456. { support result for delphi modes }
  1457. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1458. begin
  1459. oper.SetUpResult;
  1460. Consume(AS_ID);
  1461. end
  1462. { probably a variable or normal expression }
  1463. { or a procedure (such as in CALL ID) }
  1464. else
  1465. Begin
  1466. { is it a constant ? }
  1467. if SearchIConstant(actasmpattern,l) then
  1468. Begin
  1469. case oper.opr.typ of
  1470. OPR_REFERENCE :
  1471. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1472. OPR_LOCAL :
  1473. inc(oper.opr.localsymofs,BuildRefConstExpression);
  1474. OPR_NONE,
  1475. OPR_CONSTANT :
  1476. BuildConstantOperand(oper);
  1477. else
  1478. Message(asmr_e_invalid_operand_type);
  1479. end;
  1480. end
  1481. else
  1482. { Check for pascal label }
  1483. if SearchLabel(actasmpattern,hl,false) then
  1484. begin
  1485. Consume(AS_ID);
  1486. AddLabelOperand(hl);
  1487. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1488. Message(asmr_e_syntax_error);
  1489. end
  1490. else
  1491. { is it a normal variable ? }
  1492. Begin
  1493. expr:=actasmpattern;
  1494. Consume(AS_ID);
  1495. { typecasting? }
  1496. if SearchType(expr,typesize) then
  1497. begin
  1498. oper.hastype:=true;
  1499. if (actasmtoken=AS_LPAREN) then
  1500. begin
  1501. Consume(AS_LPAREN);
  1502. BuildOperand(oper);
  1503. Consume(AS_RPAREN);
  1504. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  1505. oper.SetSize(typesize,true);
  1506. end;
  1507. end
  1508. else
  1509. begin
  1510. if not oper.SetupVar(expr,false) then
  1511. Begin
  1512. { not a variable, check special variables.. }
  1513. if expr = 'SELF' then
  1514. oper.SetupSelf
  1515. else
  1516. Message1(sym_e_unknown_id,expr);
  1517. expr:='';
  1518. end;
  1519. end;
  1520. end;
  1521. end;
  1522. end;
  1523. AS_REGISTER : { Register, a variable reference or a constant reference }
  1524. begin
  1525. { save the type of register used. }
  1526. tempreg:=actasmregister;
  1527. Consume(AS_REGISTER);
  1528. if actasmtoken = AS_COLON then
  1529. Begin
  1530. Consume(AS_COLON);
  1531. oper.InitRef;
  1532. oper.opr.ref.segment:=tempreg;
  1533. BuildReference(oper);
  1534. end
  1535. else
  1536. { Simple register }
  1537. begin
  1538. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1539. Message(asmr_e_invalid_operand_type);
  1540. oper.opr.typ:=OPR_REGISTER;
  1541. oper.opr.reg:=tempreg;
  1542. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1543. end;
  1544. end;
  1545. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1546. Begin
  1547. BuildReference(oper);
  1548. end;
  1549. AS_SEG :
  1550. Begin
  1551. Message(asmr_e_seg_not_supported);
  1552. Consume(actasmtoken);
  1553. end;
  1554. AS_SEPARATOR,
  1555. AS_END,
  1556. AS_COMMA:
  1557. break;
  1558. else
  1559. Message(asmr_e_syn_operand);
  1560. end;
  1561. until not(actasmtoken in [AS_DOT,AS_PLUS,AS_LBRACKET]);
  1562. if not((actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) or
  1563. (oper.hastype and (actasmtoken=AS_RPAREN))) then
  1564. begin
  1565. Message(asmr_e_syntax_error);
  1566. RecoverConsume(true);
  1567. end;
  1568. end;
  1569. Procedure ti386intreader.BuildOpCode(instr : tx86instruction);
  1570. var
  1571. PrefixOp,OverrideOp: tasmop;
  1572. size,
  1573. operandnum : longint;
  1574. Begin
  1575. PrefixOp:=A_None;
  1576. OverrideOp:=A_None;
  1577. { prefix seg opcode / prefix opcode }
  1578. repeat
  1579. if is_prefix(actopcode) then
  1580. begin
  1581. with instr do
  1582. begin
  1583. OpOrder:=op_intel;
  1584. PrefixOp:=ActOpcode;
  1585. opcode:=ActOpcode;
  1586. condition:=ActCondition;
  1587. opsize:=ActOpsize;
  1588. ConcatInstruction(curlist);
  1589. end;
  1590. Consume(AS_OPCODE);
  1591. end
  1592. else
  1593. if is_override(actopcode) then
  1594. begin
  1595. with instr do
  1596. begin
  1597. OpOrder:=op_intel;
  1598. OverrideOp:=ActOpcode;
  1599. opcode:=ActOpcode;
  1600. condition:=ActCondition;
  1601. opsize:=ActOpsize;
  1602. ConcatInstruction(curlist);
  1603. end;
  1604. Consume(AS_OPCODE);
  1605. end
  1606. else
  1607. break;
  1608. { allow for newline after prefix or override }
  1609. while actasmtoken=AS_SEPARATOR do
  1610. Consume(AS_SEPARATOR);
  1611. until (actasmtoken<>AS_OPCODE);
  1612. { opcode }
  1613. if (actasmtoken <> AS_OPCODE) then
  1614. Begin
  1615. Message(asmr_e_invalid_or_missing_opcode);
  1616. RecoverConsume(false);
  1617. exit;
  1618. end;
  1619. { Fill the instr object with the current state }
  1620. with instr do
  1621. begin
  1622. OpOrder:=op_intel;
  1623. Opcode:=ActOpcode;
  1624. condition:=ActCondition;
  1625. opsize:=ActOpsize;
  1626. { Valid combination of prefix/override and instruction ? }
  1627. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1628. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1629. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1630. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1631. end;
  1632. { We are reading operands, so opcode will be an AS_ID }
  1633. operandnum:=1;
  1634. Consume(AS_OPCODE);
  1635. { Zero operand opcode ? }
  1636. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1637. begin
  1638. operandnum:=0;
  1639. exit;
  1640. end;
  1641. { Read Operands }
  1642. repeat
  1643. case actasmtoken of
  1644. { End of asm operands for this opcode }
  1645. AS_END,
  1646. AS_SEPARATOR :
  1647. break;
  1648. { Operand delimiter }
  1649. AS_COMMA :
  1650. Begin
  1651. if operandnum > Max_Operands then
  1652. Message(asmr_e_too_many_operands)
  1653. else
  1654. Inc(operandnum);
  1655. Consume(AS_COMMA);
  1656. end;
  1657. { Typecast, Constant Expression, Type Specifier }
  1658. AS_DWORD,
  1659. AS_BYTE,
  1660. AS_WORD,
  1661. AS_TBYTE,
  1662. AS_DQWORD,
  1663. AS_QWORD :
  1664. Begin
  1665. { load the size in a temp variable, so it can be set when the
  1666. operand is read }
  1667. size:=0;
  1668. Case actasmtoken of
  1669. AS_DWORD : size:=4;
  1670. AS_WORD : size:=2;
  1671. AS_BYTE : size:=1;
  1672. AS_QWORD : size:=8;
  1673. AS_DQWORD : size:=16;
  1674. AS_TBYTE : size:=10;
  1675. end;
  1676. Consume(actasmtoken);
  1677. case actasmtoken of
  1678. AS_LPAREN :
  1679. begin
  1680. instr.Operands[operandnum].hastype:=true;
  1681. Consume(AS_LPAREN);
  1682. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1683. Consume(AS_RPAREN);
  1684. end;
  1685. AS_PTR :
  1686. begin
  1687. Consume(AS_PTR);
  1688. instr.Operands[operandnum].InitRef;
  1689. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1690. end;
  1691. else
  1692. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1693. end;
  1694. { now set the size which was specified by the override }
  1695. instr.Operands[operandnum].setsize(size,true);
  1696. end;
  1697. { Type specifier }
  1698. AS_NEAR,
  1699. AS_FAR :
  1700. Begin
  1701. if actasmtoken = AS_NEAR then
  1702. begin
  1703. Message(asmr_w_near_ignored);
  1704. instr.opsize:=S_NEAR;
  1705. end
  1706. else
  1707. begin
  1708. Message(asmr_w_far_ignored);
  1709. instr.opsize:=S_FAR;
  1710. end;
  1711. Consume(actasmtoken);
  1712. if actasmtoken=AS_PTR then
  1713. begin
  1714. Consume(AS_PTR);
  1715. instr.Operands[operandnum].InitRef;
  1716. end;
  1717. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1718. end;
  1719. else
  1720. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1721. end; { end case }
  1722. until false;
  1723. instr.Ops:=operandnum;
  1724. end;
  1725. Procedure ti386intreader.BuildConstant(constsize: longint);
  1726. var
  1727. asmsym,
  1728. expr: string;
  1729. value : aint;
  1730. Begin
  1731. Repeat
  1732. Case actasmtoken of
  1733. AS_STRING:
  1734. Begin
  1735. { DD and DW cases }
  1736. if constsize <> 1 then
  1737. Begin
  1738. if Not PadZero(actasmpattern,constsize) then
  1739. Message(scan_f_string_exceeds_line);
  1740. end;
  1741. expr:=actasmpattern;
  1742. Consume(AS_STRING);
  1743. Case actasmtoken of
  1744. AS_COMMA:
  1745. Consume(AS_COMMA);
  1746. AS_END,
  1747. AS_SEPARATOR: ;
  1748. else
  1749. Message(asmr_e_invalid_string_expression);
  1750. end;
  1751. ConcatString(curlist,expr);
  1752. end;
  1753. AS_PLUS,
  1754. AS_MINUS,
  1755. AS_LPAREN,
  1756. AS_NOT,
  1757. AS_INTNUM,
  1758. AS_ID :
  1759. Begin
  1760. BuildConstSymbolExpression(false,false,value,asmsym);
  1761. if asmsym<>'' then
  1762. begin
  1763. if constsize<>sizeof(aint) then
  1764. Message1(asmr_w_const32bit_for_address,asmsym);
  1765. ConcatConstSymbol(curlist,asmsym,value)
  1766. end
  1767. else
  1768. ConcatConstant(curlist,value,constsize);
  1769. end;
  1770. AS_COMMA:
  1771. Consume(AS_COMMA);
  1772. AS_END,
  1773. AS_SEPARATOR:
  1774. break;
  1775. else
  1776. begin
  1777. Message(asmr_e_syn_constant);
  1778. RecoverConsume(false);
  1779. end
  1780. end;
  1781. Until false;
  1782. end;
  1783. function ti386intreader.Assemble: tlinkedlist;
  1784. Var
  1785. hl : tasmlabel;
  1786. instr : Tx86Instruction;
  1787. Begin
  1788. Message1(asmr_d_start_reading,'intel');
  1789. inexpression:=FALSE;
  1790. firsttoken:=TRUE;
  1791. { sets up all opcode and register tables in uppercase
  1792. done in the construtor now
  1793. if not _asmsorted then
  1794. Begin
  1795. SetupTables;
  1796. _asmsorted:=TRUE;
  1797. end;
  1798. }
  1799. curlist:=TAAsmoutput.Create;
  1800. { setup label linked list }
  1801. LocalLabelList:=TLocalLabelList.Create;
  1802. { start tokenizer }
  1803. c:=current_scanner.asmgetcharstart;
  1804. gettoken;
  1805. { main loop }
  1806. repeat
  1807. case actasmtoken of
  1808. AS_LLABEL:
  1809. Begin
  1810. if CreateLocalLabel(actasmpattern,hl,true) then
  1811. ConcatLabel(curlist,hl);
  1812. Consume(AS_LLABEL);
  1813. end;
  1814. AS_LABEL:
  1815. Begin
  1816. if SearchLabel(upper(actasmpattern),hl,true) then
  1817. ConcatLabel(curlist,hl)
  1818. else
  1819. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1820. Consume(AS_LABEL);
  1821. end;
  1822. AS_DW :
  1823. Begin
  1824. inexpression:=true;
  1825. Consume(AS_DW);
  1826. BuildConstant(2);
  1827. inexpression:=false;
  1828. end;
  1829. AS_DB :
  1830. Begin
  1831. inexpression:=true;
  1832. Consume(AS_DB);
  1833. BuildConstant(1);
  1834. inexpression:=false;
  1835. end;
  1836. AS_DD :
  1837. Begin
  1838. inexpression:=true;
  1839. Consume(AS_DD);
  1840. BuildConstant(4);
  1841. inexpression:=false;
  1842. end;
  1843. AS_ALIGN:
  1844. Begin
  1845. Consume(AS_ALIGN);
  1846. ConcatAlign(curlist,BuildConstExpression);
  1847. if actasmtoken<>AS_SEPARATOR then
  1848. Consume(AS_SEPARATOR);
  1849. end;
  1850. AS_OPCODE :
  1851. Begin
  1852. instr:=Tx86Instruction.Create(Tx86Operand);
  1853. BuildOpcode(instr);
  1854. with instr do
  1855. begin
  1856. { We need AT&T style operands }
  1857. Swapoperands;
  1858. { Must be done with args in ATT order }
  1859. CheckNonCommutativeOpcodes;
  1860. AddReferenceSizes;
  1861. SetInstructionOpsize;
  1862. CheckOperandSizes;
  1863. ConcatInstruction(curlist);
  1864. end;
  1865. instr.Free;
  1866. end;
  1867. AS_SEPARATOR :
  1868. Begin
  1869. Consume(AS_SEPARATOR);
  1870. end;
  1871. AS_END :
  1872. break; { end assembly block }
  1873. else
  1874. Begin
  1875. Message(asmr_e_syntax_error);
  1876. RecoverConsume(false);
  1877. end;
  1878. end; { end case }
  1879. until false;
  1880. { Check LocalLabelList }
  1881. LocalLabelList.CheckEmitted;
  1882. LocalLabelList.Free;
  1883. { Return the list in an asmnode }
  1884. assemble:=curlist;
  1885. Message1(asmr_d_finish_reading,'intel');
  1886. end;
  1887. {*****************************************************************************
  1888. Initialize
  1889. *****************************************************************************}
  1890. const
  1891. asmmode_i386_intel_info : tasmmodeinfo =
  1892. (
  1893. id : asmmode_i386_intel;
  1894. idtxt : 'INTEL';
  1895. casmreader : ti386intreader;
  1896. );
  1897. begin
  1898. RegisterAsmMode(asmmode_i386_intel_info);
  1899. end.
  1900. {
  1901. $Log$
  1902. Revision 1.80 2004-11-09 22:32:59 peter
  1903. * small m68k updates to bring it up2date
  1904. * give better error for external local variable
  1905. Revision 1.79 2004/11/08 22:09:59 peter
  1906. * tvarsym splitted
  1907. Revision 1.78 2004/10/31 21:45:03 peter
  1908. * generic tlocation
  1909. * move tlocation to cgutils
  1910. Revision 1.77 2004/09/13 20:25:52 peter
  1911. * support byte() typecast
  1912. * support array index
  1913. Revision 1.76 2004/07/06 19:47:19 peter
  1914. * fixed parsing of strings in db
  1915. Revision 1.75 2004/06/23 14:54:46 peter
  1916. * align directive added
  1917. Revision 1.74 2004/06/20 08:55:31 florian
  1918. * logs truncated
  1919. Revision 1.73 2004/06/16 20:07:10 florian
  1920. * dwarf branch merged
  1921. Revision 1.72 2004/05/20 21:54:33 florian
  1922. + <pointer> - <pointer> result is divided by the pointer element size now
  1923. this is delphi compatible as well as resulting in the expected result for p1+(p2-p1)
  1924. Revision 1.71.2.3 2004/05/02 00:31:33 peter
  1925. * fixedi i386 compile
  1926. Revision 1.71.2.2 2004/05/01 23:36:47 peter
  1927. * assembler reader 64bit fixes
  1928. Revision 1.71.2.1 2004/05/01 16:02:10 peter
  1929. * POINTER_SIZE replaced with sizeof(aint)
  1930. * aint,aword,tconst*int moved to globtype
  1931. }