rax86int.pas 89 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing process for the intel styled inline assembler.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. Unit Rax86int;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. cpubase,
  23. globtype,
  24. aasmbase,
  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_DQ,AS_END,
  35. {------------------ Assembler Operators --------------------}
  36. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_NEAR,AS_FAR,
  37. AS_HIGH,AS_LOW,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  38. AS_AND,AS_OR,AS_XOR,AS_WRT,AS___GOTPCREL);
  39. type
  40. tx86intreader = class(tasmreader)
  41. actasmtoken : tasmtoken;
  42. prevasmtoken : tasmtoken;
  43. ActOpsize : topsize;
  44. inexpression : boolean;
  45. constructor create;override;
  46. function is_asmopcode(const s: string):boolean;
  47. function is_asmoperator(const s: string):boolean;
  48. function is_asmdirective(const s: string):boolean;
  49. function is_register(const s:string):boolean;
  50. function is_locallabel(const s:string):boolean;
  51. function Assemble: tlinkedlist;override;
  52. procedure GetToken;
  53. function consume(t : tasmtoken):boolean;
  54. procedure RecoverConsume(allowcomma:boolean);
  55. procedure BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  56. procedure BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype;out isseg,is_farproc_entry:boolean);
  57. function BuildConstExpression:aint;
  58. function BuildRefConstExpression(startingminus:boolean=false):aint;
  59. procedure BuildReference(oper : tx86operand);
  60. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  61. procedure BuildConstantOperand(oper: tx86operand);
  62. procedure BuildOpCode(instr : tx86instruction);
  63. procedure BuildConstant(constsize: byte);
  64. end;
  65. implementation
  66. uses
  67. { common }
  68. cutils,
  69. { global }
  70. globals,verbose,
  71. systems,cpuinfo,
  72. { aasm }
  73. aasmtai,aasmdata,aasmcpu,
  74. { symtable }
  75. symconst,symbase,symtype,symsym,symdef,symtable,symcpu,
  76. { parser }
  77. scanner,pbase,
  78. { register allocator }
  79. rabase,rautils,itx86int,
  80. { codegen }
  81. cgbase,cgobj,procinfo
  82. ;
  83. type
  84. tasmkeyword = string[9];
  85. const
  86. { These tokens should be modified accordingly to the modifications }
  87. { in the different enumerations. }
  88. firstdirective = AS_ALIGN;
  89. lastdirective = AS_END;
  90. firstoperator = AS_BYTE;
  91. lastoperator = AS___GOTPCREL;
  92. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  93. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  94. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  95. ('ALIGN','DB','DW','DD','DQ','END');
  96. { problems with shl,shr,not,and,or and xor, they are }
  97. { context sensitive. }
  98. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  99. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','OWORD','XMMWORD','YWORD','YMMWORD','NEAR','FAR','HIGH',
  100. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  101. 'OR','XOR','WRT','GOTPCREL');
  102. token2str : array[tasmtoken] of string[10] = (
  103. '','Label','LLabel','String','Integer',
  104. ',',',',',',',',',','[',']','(',
  105. ')',':','.','+','-','*',
  106. ';','identifier','register','opcode','/',
  107. '','','','','','END',
  108. '','','','','','','','','',
  109. '','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  110. 'and','or','xor','wrt','..gotpcrel'
  111. );
  112. constructor tx86intreader.create;
  113. var
  114. i : tasmop;
  115. Begin
  116. inherited create;
  117. iasmops:=TFPHashList.create;
  118. for i:=firstop to lastop do
  119. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  120. end;
  121. {---------------------------------------------------------------------}
  122. { Routines for the tokenizing }
  123. {---------------------------------------------------------------------}
  124. function tx86intreader.is_asmopcode(const s: string):boolean;
  125. var
  126. cond : string[4];
  127. cnd : tasmcond;
  128. j: longint;
  129. Begin
  130. is_asmopcode:=FALSE;
  131. actopcode:=A_None;
  132. actcondition:=C_None;
  133. actopsize:=S_NO;
  134. { Search opcodes }
  135. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  136. if actopcode<>A_NONE then
  137. begin
  138. actasmtoken:=AS_OPCODE;
  139. result:=TRUE;
  140. exit;
  141. end;
  142. { not found yet, check condition opcodes }
  143. j:=0;
  144. while (j<CondAsmOps) do
  145. begin
  146. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  147. begin
  148. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  149. if cond<>'' then
  150. begin
  151. for cnd:=low(TasmCond) to high(TasmCond) do
  152. if Cond=Upper(cond2str[cnd]) then
  153. begin
  154. actopcode:=CondASmOp[j];
  155. actcondition:=cnd;
  156. is_asmopcode:=TRUE;
  157. actasmtoken:=AS_OPCODE;
  158. exit
  159. end;
  160. end;
  161. end;
  162. inc(j);
  163. end;
  164. end;
  165. function tx86intreader.is_asmoperator(const s: string):boolean;
  166. var
  167. i : longint;
  168. Begin
  169. for i:=0 to _count_asmoperators do
  170. if s=_asmoperators[i] then
  171. begin
  172. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  173. is_asmoperator:=true;
  174. exit;
  175. end;
  176. is_asmoperator:=false;
  177. end;
  178. Function tx86intreader.is_asmdirective(const s: string):boolean;
  179. var
  180. i : longint;
  181. Begin
  182. for i:=0 to _count_asmdirectives do
  183. if s=_asmdirectives[i] then
  184. begin
  185. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  186. is_asmdirective:=true;
  187. exit;
  188. end;
  189. is_asmdirective:=false;
  190. end;
  191. function tx86intreader.is_register(const s:string):boolean;
  192. var
  193. entry: TSymEntry;
  194. begin
  195. is_register:=false;
  196. actasmregister:=masm_regnum_search(lower(s));
  197. { don't acceps "flags" as register name in an instruction }
  198. if actasmregister=NR_FLAGS then
  199. actasmregister:=NR_NO;
  200. if (actasmregister=NR_NO) and
  201. (current_procinfo.procdef.proccalloption=pocall_register) and
  202. (po_assembler in current_procinfo.procdef.procoptions) then
  203. begin
  204. entry:=current_procinfo.procdef.parast.Find(s);
  205. if assigned(entry) and
  206. (entry.typ=paravarsym) and
  207. assigned(tparavarsym(entry).paraloc[calleeside].Location) and
  208. (tparavarsym(entry).paraloc[calleeside].Location^.Loc=LOC_REGISTER) then
  209. actasmregister:=tparavarsym(entry).paraloc[calleeside].Location^.register;
  210. end;
  211. if actasmregister<>NR_NO then
  212. begin
  213. is_register:=true;
  214. actasmtoken:=AS_REGISTER;
  215. end;
  216. end;
  217. function tx86intreader.is_locallabel(const s:string):boolean;
  218. begin
  219. is_locallabel:=(length(s)>1) and (s[1]='@');
  220. end;
  221. Procedure tx86intreader.GetToken;
  222. var
  223. len : longint;
  224. forcelabel : boolean;
  225. srsym : tsym;
  226. srsymtable : TSymtable;
  227. begin
  228. c:=scanner.c;
  229. { save old token and reset new token }
  230. prevasmtoken:=actasmtoken;
  231. actasmtoken:=AS_NONE;
  232. { reset }
  233. forcelabel:=FALSE;
  234. actasmpattern:='';
  235. { while space and tab , continue scan... }
  236. while (c in [' ',#9]) do
  237. c:=current_scanner.asmgetchar;
  238. { get token pos }
  239. if not (c in [#10,#13,'{',';','(','/']) then
  240. current_scanner.gettokenpos;
  241. { Local Label, Label, Directive, Prefix or Opcode }
  242. if firsttoken and not (c in [#10,#13,'{',';','(','/']) then
  243. begin
  244. firsttoken:=FALSE;
  245. len:=0;
  246. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  247. begin
  248. { if there is an at_sign, then this must absolutely be a label }
  249. if c = '@' then
  250. forcelabel:=TRUE;
  251. inc(len);
  252. actasmpattern[len]:=c;
  253. c:=current_scanner.asmgetchar;
  254. end;
  255. actasmpattern[0]:=chr(len);
  256. uppervar(actasmpattern);
  257. { allow spaces }
  258. while (c in [' ',#9]) do
  259. c:=current_scanner.asmgetchar;
  260. { label ? }
  261. if c = ':' then
  262. begin
  263. if actasmpattern[1]='@' then
  264. actasmtoken:=AS_LLABEL
  265. else
  266. actasmtoken:=AS_LABEL;
  267. { let us point to the next character }
  268. c:=current_scanner.asmgetchar;
  269. firsttoken:=true;
  270. exit;
  271. end;
  272. { Are we trying to create an identifier with }
  273. { an at-sign...? }
  274. if forcelabel then
  275. Message(asmr_e_none_label_contain_at);
  276. { opcode ? }
  277. If is_asmopcode(actasmpattern) then
  278. Begin
  279. { check if we are in an expression }
  280. { then continue with asm directives }
  281. if not inexpression then
  282. exit;
  283. end;
  284. if is_asmdirective(actasmpattern) then
  285. exit;
  286. message1(asmr_e_unknown_opcode,actasmpattern);
  287. actasmtoken:=AS_NONE;
  288. exit;
  289. end
  290. else { else firsttoken }
  291. begin
  292. case c of
  293. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  294. { - @Result, @Code or @Data special variables. }
  295. begin
  296. actasmpattern:=c;
  297. c:=current_scanner.asmgetchar;
  298. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  299. begin
  300. actasmpattern:=actasmpattern + c;
  301. c:=current_scanner.asmgetchar;
  302. end;
  303. uppervar(actasmpattern);
  304. actasmtoken:=AS_ID;
  305. exit;
  306. end;
  307. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  308. begin
  309. actasmpattern:=c;
  310. c:=current_scanner.asmgetchar;
  311. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  312. begin
  313. actasmpattern:=actasmpattern + c;
  314. c:=current_scanner.asmgetchar;
  315. end;
  316. uppervar(actasmpattern);
  317. { after prefix (or segment override) we allow also a new opcode }
  318. If (is_prefix(actopcode) or is_override(actopcode)) and is_asmopcode(actasmpattern) then
  319. Begin
  320. { if we are not in a constant }
  321. { expression than this is an }
  322. { opcode. }
  323. if not inexpression then
  324. exit;
  325. end;
  326. { support st(X) for fpu registers }
  327. if (actasmpattern = 'ST') and (c='(') then
  328. Begin
  329. actasmpattern:=actasmpattern+c;
  330. c:=current_scanner.asmgetchar;
  331. { allow spaces }
  332. while (c in [' ',#9]) do
  333. c:=current_scanner.asmgetchar;
  334. if c in ['0'..'7'] then
  335. actasmpattern:=actasmpattern + c
  336. else
  337. Message(asmr_e_invalid_fpu_register);
  338. c:=current_scanner.asmgetchar;
  339. { allow spaces }
  340. while (c in [' ',#9]) do
  341. c:=current_scanner.asmgetchar;
  342. if c <> ')' then
  343. Message(asmr_e_invalid_fpu_register)
  344. else
  345. Begin
  346. actasmpattern:=actasmpattern + c;
  347. c:=current_scanner.asmgetchar;
  348. end;
  349. end;
  350. if is_asmdirective(actasmpattern) then
  351. exit;
  352. if is_asmoperator(actasmpattern) then
  353. exit;
  354. if is_register(actasmpattern) then
  355. exit;
  356. { allow spaces }
  357. while (c in [' ',#9]) do
  358. c:=current_scanner.asmgetchar;
  359. { if next is a '.' and this is a unitsym then we also need to
  360. parse the identifier }
  361. if (c='.') then
  362. begin
  363. asmsearchsym(actasmpattern,srsym,srsymtable);
  364. if assigned(srsym) and
  365. (srsym.typ=unitsym) and
  366. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  367. srsym.owner.iscurrentunit then
  368. begin
  369. { Add . to create System.Identifier }
  370. actasmpattern:=actasmpattern+c;
  371. c:=current_scanner.asmgetchar;
  372. { Delphi allows System.@Halt, just ignore the @ }
  373. if c='@' then
  374. c:=current_scanner.asmgetchar;
  375. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  376. begin
  377. actasmpattern:=actasmpattern + upcase(c);
  378. c:=current_scanner.asmgetchar;
  379. end;
  380. end;
  381. end;
  382. actasmtoken:=AS_ID;
  383. exit;
  384. end;
  385. '''' : { string or character }
  386. begin
  387. actasmpattern:='';
  388. repeat
  389. if c = '''' then
  390. begin
  391. c:=current_scanner.asmgetchar;
  392. if c in [#10,#13] then
  393. begin
  394. Message(scan_f_string_exceeds_line);
  395. break;
  396. end;
  397. repeat
  398. if c='''' then
  399. begin
  400. c:=current_scanner.asmgetchar;
  401. if c='''' then
  402. begin
  403. actasmpattern:=actasmpattern+'''';
  404. c:=current_scanner.asmgetchar;
  405. if c in [#10,#13] then
  406. begin
  407. Message(scan_f_string_exceeds_line);
  408. break;
  409. end;
  410. end
  411. else
  412. break;
  413. end
  414. else
  415. begin
  416. actasmpattern:=actasmpattern+c;
  417. c:=current_scanner.asmgetchar;
  418. if c in [#10,#13] then
  419. begin
  420. Message(scan_f_string_exceeds_line);
  421. break
  422. end;
  423. end;
  424. until false; { end repeat }
  425. end
  426. else
  427. break; { end if }
  428. until false;
  429. actasmtoken:=AS_STRING;
  430. exit;
  431. end;
  432. '"' : { string or character }
  433. begin
  434. actasmpattern:='';
  435. repeat
  436. if c = '"' then
  437. begin
  438. c:=current_scanner.asmgetchar;
  439. if c in [#10,#13] then
  440. begin
  441. Message(scan_f_string_exceeds_line);
  442. break;
  443. end;
  444. repeat
  445. if c='"' then
  446. begin
  447. c:=current_scanner.asmgetchar;
  448. if c='"' then
  449. begin
  450. actasmpattern:=actasmpattern+'"';
  451. c:=current_scanner.asmgetchar;
  452. if c in [#10,#13] then
  453. begin
  454. Message(scan_f_string_exceeds_line);
  455. break;
  456. end;
  457. end
  458. else
  459. break;
  460. end
  461. else
  462. begin
  463. actasmpattern:=actasmpattern+c;
  464. c:=current_scanner.asmgetchar;
  465. if c in [#10,#13] then
  466. begin
  467. Message(scan_f_string_exceeds_line);
  468. break
  469. end;
  470. end;
  471. until false; { end repeat }
  472. end
  473. else
  474. break; { end if }
  475. until false;
  476. actasmtoken:=AS_STRING;
  477. exit;
  478. end;
  479. '$' :
  480. begin
  481. c:=current_scanner.asmgetchar;
  482. while c in ['0'..'9','A'..'F','a'..'f'] do
  483. begin
  484. actasmpattern:=actasmpattern + c;
  485. c:=current_scanner.asmgetchar;
  486. end;
  487. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  488. actasmtoken:=AS_INTNUM;
  489. exit;
  490. end;
  491. '&' : { identifier }
  492. begin
  493. actasmpattern:='';
  494. c:=current_scanner.asmgetchar;
  495. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  496. begin
  497. actasmpattern:=actasmpattern + c;
  498. c:=current_scanner.asmgetchar;
  499. end;
  500. uppervar(actasmpattern);
  501. actasmtoken:=AS_ID;
  502. exit;
  503. end;
  504. ',' :
  505. begin
  506. actasmtoken:=AS_COMMA;
  507. c:=current_scanner.asmgetchar;
  508. exit;
  509. end;
  510. '[' :
  511. begin
  512. actasmtoken:=AS_LBRACKET;
  513. c:=current_scanner.asmgetchar;
  514. exit;
  515. end;
  516. ']' :
  517. begin
  518. actasmtoken:=AS_RBRACKET;
  519. c:=current_scanner.asmgetchar;
  520. exit;
  521. end;
  522. '(' :
  523. begin
  524. c:=current_scanner.asmgetchar;
  525. if c='*' then
  526. begin
  527. current_scanner.skipoldtpcomment(true);
  528. GetToken;
  529. end
  530. else
  531. actasmtoken:=AS_LPAREN;
  532. exit;
  533. end;
  534. ')' :
  535. begin
  536. actasmtoken:=AS_RPAREN;
  537. c:=current_scanner.asmgetchar;
  538. exit;
  539. end;
  540. ':' :
  541. begin
  542. actasmtoken:=AS_COLON;
  543. c:=current_scanner.asmgetchar;
  544. exit;
  545. end;
  546. '.' :
  547. begin
  548. c:=current_scanner.asmgetchar;
  549. {$ifdef x86_64}
  550. if c='.' then
  551. begin
  552. actasmpattern:='..';
  553. c:=current_scanner.asmgetchar;
  554. repeat
  555. actasmpattern:=actasmpattern+c;
  556. c:=current_scanner.asmgetchar;
  557. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  558. if upper(actasmpattern)<>'..GOTPCREL' then
  559. begin
  560. actasmtoken:=AS_ID;
  561. consume(AS___GOTPCREL);
  562. end;
  563. actasmtoken:=AS___GOTPCREL
  564. end
  565. else
  566. {$endif x86_64}
  567. actasmtoken:=AS_DOT;
  568. exit;
  569. end;
  570. '+' :
  571. begin
  572. actasmtoken:=AS_PLUS;
  573. c:=current_scanner.asmgetchar;
  574. exit;
  575. end;
  576. '-' :
  577. begin
  578. actasmtoken:=AS_MINUS;
  579. c:=current_scanner.asmgetchar;
  580. exit;
  581. end;
  582. '*' :
  583. begin
  584. actasmtoken:=AS_STAR;
  585. c:=current_scanner.asmgetchar;
  586. exit;
  587. end;
  588. '/' :
  589. begin
  590. c:=current_scanner.asmgetchar;
  591. if c='/' then
  592. begin
  593. current_scanner.skipdelphicomment;
  594. GetToken;
  595. end
  596. else
  597. actasmtoken:=AS_SLASH;
  598. exit;
  599. end;
  600. '0'..'9':
  601. begin
  602. actasmpattern:=c;
  603. c:=current_scanner.asmgetchar;
  604. { Get the possible characters }
  605. while c in ['0'..'9','A'..'F','a'..'f'] do
  606. begin
  607. actasmpattern:=actasmpattern + c;
  608. c:=current_scanner.asmgetchar;
  609. end;
  610. { Get ending character }
  611. uppervar(actasmpattern);
  612. c:=upcase(c);
  613. { possibly a binary number. }
  614. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  615. Begin
  616. { Delete the last binary specifier }
  617. delete(actasmpattern,length(actasmpattern),1);
  618. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  619. actasmtoken:=AS_INTNUM;
  620. exit;
  621. end
  622. else
  623. Begin
  624. case c of
  625. 'O' :
  626. Begin
  627. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  628. actasmtoken:=AS_INTNUM;
  629. c:=current_scanner.asmgetchar;
  630. exit;
  631. end;
  632. 'H' :
  633. Begin
  634. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  635. actasmtoken:=AS_INTNUM;
  636. c:=current_scanner.asmgetchar;
  637. exit;
  638. end;
  639. else { must be an integer number }
  640. begin
  641. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  642. actasmtoken:=AS_INTNUM;
  643. exit;
  644. end;
  645. end;
  646. end;
  647. end;
  648. #13,#10:
  649. begin
  650. current_scanner.linebreak;
  651. c:=current_scanner.asmgetchar;
  652. firsttoken:=TRUE;
  653. actasmtoken:=AS_SEPARATOR;
  654. exit;
  655. end;
  656. ';':
  657. begin
  658. c:=current_scanner.asmgetchar;
  659. firsttoken:=TRUE;
  660. actasmtoken:=AS_SEPARATOR;
  661. exit;
  662. end;
  663. '{':
  664. begin
  665. current_scanner.skipcomment(true);
  666. GetToken;
  667. end;
  668. else
  669. current_scanner.illegal_char(c);
  670. end;
  671. end;
  672. end;
  673. function tx86intreader.consume(t : tasmtoken):boolean;
  674. begin
  675. Consume:=true;
  676. if t<>actasmtoken then
  677. begin
  678. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  679. Consume:=false;
  680. end;
  681. repeat
  682. gettoken;
  683. until actasmtoken<>AS_NONE;
  684. end;
  685. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  686. begin
  687. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  688. begin
  689. if allowcomma and (actasmtoken=AS_COMMA) then
  690. break;
  691. Consume(actasmtoken);
  692. end;
  693. end;
  694. {*****************************************************************************
  695. Parsing Helpers
  696. *****************************************************************************}
  697. { This routine builds up a record offset after a AS_DOT
  698. token is encountered.
  699. On entry actasmtoken should be equal to AS_DOT }
  700. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  701. var
  702. s: string;
  703. Begin
  704. offset:=0;
  705. size:=0;
  706. s:=expr;
  707. while (actasmtoken=AS_DOT) do
  708. begin
  709. Consume(AS_DOT);
  710. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_REGISTER] then
  711. begin
  712. s:=s+'.'+actasmpattern;
  713. consume(actasmtoken);
  714. end
  715. else
  716. begin
  717. Consume(AS_ID);
  718. RecoverConsume(true);
  719. break;
  720. end;
  721. end;
  722. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs) then
  723. Message(asmr_e_building_record_offset);
  724. end;
  725. Procedure tx86intreader.BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype;out isseg,is_farproc_entry:boolean);
  726. var
  727. tempstr,expr,hs,mangledname : string;
  728. parenlevel : longint;
  729. l,k : aint;
  730. hasparen,
  731. errorflag,
  732. needvmtofs : boolean;
  733. prevtok : tasmtoken;
  734. hl : tasmlabel;
  735. hssymtyp : Tasmsymtype;
  736. def : tdef;
  737. sym : tsym;
  738. srsymtable : TSymtable;
  739. Begin
  740. { reset }
  741. value:=0;
  742. asmsym:='';
  743. asmsymtyp:=AT_DATA;
  744. isseg:=false;
  745. is_farproc_entry:=FALSE;
  746. errorflag:=FALSE;
  747. tempstr:='';
  748. expr:='';
  749. if startingminus then
  750. expr:='-';
  751. inexpression:=TRUE;
  752. parenlevel:=0;
  753. sym:=nil;
  754. needvmtofs:=FALSE;
  755. Repeat
  756. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  757. if isref and (actasmtoken=AS_LBRACKET) then
  758. break;
  759. Case actasmtoken of
  760. AS_LPAREN:
  761. Begin
  762. Consume(AS_LPAREN);
  763. expr:=expr + '(';
  764. inc(parenlevel);
  765. end;
  766. AS_RPAREN:
  767. Begin
  768. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  769. if parenlevel=0 then
  770. break;
  771. Consume(AS_RPAREN);
  772. expr:=expr + ')';
  773. dec(parenlevel);
  774. end;
  775. AS_SHL:
  776. Begin
  777. Consume(AS_SHL);
  778. expr:=expr + '<';
  779. end;
  780. AS_SHR:
  781. Begin
  782. Consume(AS_SHR);
  783. expr:=expr + '>';
  784. end;
  785. AS_SLASH:
  786. Begin
  787. Consume(AS_SLASH);
  788. expr:=expr + '/';
  789. end;
  790. AS_MOD:
  791. Begin
  792. Consume(AS_MOD);
  793. expr:=expr + '%';
  794. end;
  795. AS_STAR:
  796. Begin
  797. Consume(AS_STAR);
  798. if isref and (actasmtoken=AS_REGISTER) then
  799. break;
  800. expr:=expr + '*';
  801. end;
  802. AS_PLUS:
  803. Begin
  804. Consume(AS_PLUS);
  805. if isref and (actasmtoken=AS_REGISTER) then
  806. break;
  807. expr:=expr + '+';
  808. end;
  809. AS_MINUS:
  810. Begin
  811. Consume(AS_MINUS);
  812. expr:=expr + '-';
  813. end;
  814. AS_AND:
  815. Begin
  816. Consume(AS_AND);
  817. expr:=expr + '&';
  818. end;
  819. AS_NOT:
  820. Begin
  821. Consume(AS_NOT);
  822. expr:=expr + '~';
  823. end;
  824. AS_XOR:
  825. Begin
  826. Consume(AS_XOR);
  827. expr:=expr + '^';
  828. end;
  829. AS_OR:
  830. Begin
  831. Consume(AS_OR);
  832. expr:=expr + '|';
  833. end;
  834. AS_INTNUM:
  835. Begin
  836. expr:=expr + actasmpattern;
  837. Consume(AS_INTNUM);
  838. end;
  839. {$ifdef i8086}
  840. AS_SEG:
  841. begin
  842. isseg:=true;
  843. Consume(actasmtoken);
  844. if actasmtoken<>AS_ID then
  845. Message(asmr_e_seg_without_identifier);
  846. end;
  847. {$endif i8086}
  848. AS_VMTOFFSET,
  849. AS_OFFSET:
  850. begin
  851. if (actasmtoken = AS_OFFSET) then
  852. needofs:=true
  853. else
  854. needvmtofs:=true;
  855. Consume(actasmtoken);
  856. if actasmtoken<>AS_ID then
  857. Message(asmr_e_offset_without_identifier);
  858. end;
  859. AS_SIZEOF,
  860. AS_TYPE:
  861. begin
  862. l:=0;
  863. hasparen:=false;
  864. Consume(actasmtoken);
  865. if actasmtoken=AS_LPAREN then
  866. begin
  867. hasparen:=true;
  868. Consume(AS_LPAREN);
  869. end;
  870. if actasmtoken<>AS_ID then
  871. Message(asmr_e_type_without_identifier)
  872. else
  873. begin
  874. tempstr:=actasmpattern;
  875. Consume(AS_ID);
  876. if actasmtoken=AS_DOT then
  877. begin
  878. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  879. if mangledname<>'' then
  880. { procsym }
  881. Message(asmr_e_wrong_sym_type);
  882. end
  883. else
  884. begin
  885. asmsearchsym(tempstr,sym,srsymtable);
  886. if assigned(sym) then
  887. begin
  888. case sym.typ of
  889. staticvarsym,
  890. localvarsym,
  891. paravarsym :
  892. l:=tabstractvarsym(sym).getsize;
  893. typesym :
  894. l:=ttypesym(sym).typedef.size;
  895. else
  896. Message(asmr_e_wrong_sym_type);
  897. end;
  898. end
  899. else
  900. Message1(sym_e_unknown_id,tempstr);
  901. end;
  902. end;
  903. str(l, tempstr);
  904. expr:=expr + tempstr;
  905. if hasparen then
  906. Consume(AS_RPAREN);
  907. end;
  908. AS_PTR :
  909. begin
  910. { Support ugly delphi constructs like <constant> PTR [ref] }
  911. break;
  912. end;
  913. AS_STRING:
  914. begin
  915. l:=0;
  916. case Length(actasmpattern) of
  917. 1 :
  918. l:=ord(actasmpattern[1]);
  919. 2 :
  920. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  921. 3 :
  922. l:=ord(actasmpattern[3]) +
  923. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  924. 4 :
  925. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  926. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  927. else
  928. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  929. end;
  930. str(l, tempstr);
  931. expr:=expr + tempstr;
  932. Consume(AS_STRING);
  933. end;
  934. AS_ID:
  935. begin
  936. hs:='';
  937. hssymtyp:=AT_DATA;
  938. def:=nil;
  939. tempstr:=actasmpattern;
  940. prevtok:=prevasmtoken;
  941. { stop parsing a constant expression if we find an opcode after a
  942. non-operator like "db $66 mov eax,ebx" }
  943. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  944. is_asmopcode(actasmpattern) then
  945. break;
  946. consume(AS_ID);
  947. if (tempstr='@CODE') or (tempstr='@DATA') then
  948. begin
  949. if asmsym='' then
  950. begin
  951. asmsym:=tempstr;
  952. asmsymtyp:=AT_SECTION;
  953. end
  954. else
  955. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  956. end
  957. else if SearchIConstant(tempstr,l) then
  958. begin
  959. str(l, tempstr);
  960. expr:=expr + tempstr;
  961. end
  962. else
  963. begin
  964. if is_locallabel(tempstr) then
  965. begin
  966. CreateLocalLabel(tempstr,hl,false);
  967. hs:=hl.name;
  968. hssymtyp:=AT_FUNCTION;
  969. end
  970. else
  971. if SearchLabel(tempstr,hl,false) then
  972. begin
  973. hs:=hl.name;
  974. hssymtyp:=AT_FUNCTION;
  975. end
  976. else
  977. begin
  978. asmsearchsym(tempstr,sym,srsymtable);
  979. if assigned(sym) then
  980. begin
  981. case sym.typ of
  982. staticvarsym :
  983. begin
  984. hs:=tstaticvarsym(sym).mangledname;
  985. def:=tstaticvarsym(sym).vardef;
  986. end;
  987. localvarsym,
  988. paravarsym :
  989. begin
  990. Message(asmr_e_no_local_or_para_allowed);
  991. end;
  992. procsym :
  993. begin
  994. if Tprocsym(sym).ProcdefList.Count>1 then
  995. Message(asmr_w_calling_overload_func);
  996. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  997. {$ifdef i8086}
  998. is_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  999. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  1000. {$endif i8086}
  1001. hssymtyp:=AT_FUNCTION;
  1002. end;
  1003. typesym :
  1004. begin
  1005. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1006. Message(asmr_e_wrong_sym_type);
  1007. end;
  1008. fieldvarsym :
  1009. begin
  1010. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1011. end;
  1012. else
  1013. Message(asmr_e_wrong_sym_type);
  1014. end;
  1015. end
  1016. else
  1017. Message1(sym_e_unknown_id,tempstr);
  1018. end;
  1019. { symbol found? }
  1020. if hs<>'' then
  1021. begin
  1022. if asmsym='' then
  1023. begin
  1024. asmsym:=hs;
  1025. asmsymtyp:=hssymtyp;
  1026. end
  1027. else
  1028. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1029. if (expr='') or (expr[length(expr)]='+') then
  1030. begin
  1031. { don't remove the + if there could be a record field }
  1032. if actasmtoken<>AS_DOT then
  1033. delete(expr,length(expr),1);
  1034. end
  1035. else
  1036. if needofs then
  1037. begin
  1038. if (prevtok<>AS_OFFSET) then
  1039. Message(asmr_e_need_offset);
  1040. end
  1041. else
  1042. Message(asmr_e_only_add_relocatable_symbol);
  1043. end;
  1044. if (actasmtoken=AS_DOT) or
  1045. (assigned(sym) and
  1046. (sym.typ = fieldvarsym) and
  1047. not(sp_static in sym.symoptions)) then
  1048. begin
  1049. BuildRecordOffsetSize(tempstr,l,k,hs,needvmtofs);
  1050. if hs <> '' then
  1051. hssymtyp:=AT_FUNCTION
  1052. else
  1053. begin
  1054. str(l, tempstr);
  1055. expr:=expr + tempstr;
  1056. end
  1057. end
  1058. else
  1059. begin
  1060. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1061. delete(expr,length(expr),1);
  1062. end;
  1063. if (actasmtoken=AS_LBRACKET) and
  1064. assigned(def) and
  1065. (def.typ=arraydef) then
  1066. begin
  1067. consume(AS_LBRACKET);
  1068. l:=BuildConstExpression;
  1069. if l<tarraydef(def).lowrange then
  1070. begin
  1071. Message(asmr_e_constant_out_of_bounds);
  1072. l:=0;
  1073. end
  1074. else
  1075. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1076. str(l, tempstr);
  1077. expr:=expr + '+' + tempstr;
  1078. consume(AS_RBRACKET);
  1079. end;
  1080. end;
  1081. { check if there are wrong operator used like / or mod etc. }
  1082. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1083. Message(asmr_e_only_add_relocatable_symbol);
  1084. end;
  1085. AS_ALIGN,
  1086. AS_DB,
  1087. AS_DW,
  1088. AS_DD,
  1089. AS_DQ,
  1090. AS_END,
  1091. AS_RBRACKET,
  1092. AS_SEPARATOR,
  1093. AS_COMMA,
  1094. AS_COLON:
  1095. break;
  1096. else
  1097. begin
  1098. { write error only once. }
  1099. if not errorflag then
  1100. Message(asmr_e_invalid_constant_expression);
  1101. { consume tokens until we find COMMA or SEPARATOR }
  1102. Consume(actasmtoken);
  1103. errorflag:=TRUE;
  1104. end;
  1105. end;
  1106. Until false;
  1107. { calculate expression }
  1108. if not ErrorFlag then
  1109. value:=CalculateExpression(expr)
  1110. else
  1111. value:=0;
  1112. { no longer in an expression }
  1113. inexpression:=FALSE;
  1114. end;
  1115. Function tx86intreader.BuildConstExpression:aint;
  1116. var
  1117. l : aint;
  1118. hs : string;
  1119. hssymtyp : TAsmsymtype;
  1120. isseg : boolean;
  1121. is_farproc_entry : boolean;
  1122. begin
  1123. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp,isseg,is_farproc_entry);
  1124. if hs<>'' then
  1125. Message(asmr_e_relocatable_symbol_not_allowed);
  1126. BuildConstExpression:=l;
  1127. end;
  1128. Function tx86intreader.BuildRefConstExpression(startingminus:boolean):aint;
  1129. var
  1130. l : aint;
  1131. hs : string;
  1132. hssymtyp : TAsmsymtype;
  1133. isseg : boolean;
  1134. is_farproc_entry : boolean;
  1135. begin
  1136. BuildConstSymbolExpression(false,true,startingminus,l,hs,hssymtyp,isseg,is_farproc_entry);
  1137. if hs<>'' then
  1138. Message(asmr_e_relocatable_symbol_not_allowed);
  1139. BuildRefConstExpression:=l;
  1140. end;
  1141. procedure tx86intreader.BuildReference(oper : tx86operand);
  1142. var
  1143. scale : byte;
  1144. k,l : aint;
  1145. tempstr,hs : string;
  1146. tempsymtyp : tasmsymtype;
  1147. code : integer;
  1148. hreg : tregister;
  1149. GotStar,GotOffset,HadVar,
  1150. GotPlus,Negative : boolean;
  1151. hl : tasmlabel;
  1152. isseg: boolean;
  1153. is_farproc_entry : boolean;
  1154. Begin
  1155. Consume(AS_LBRACKET);
  1156. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1157. oper.InitRef;
  1158. GotStar:=false;
  1159. GotPlus:=true;
  1160. GotOffset:=false;
  1161. Negative:=false;
  1162. Scale:=0;
  1163. repeat
  1164. if GotOffset and (actasmtoken<>AS_ID) then
  1165. Message(asmr_e_invalid_reference_syntax);
  1166. Case actasmtoken of
  1167. AS_ID, { Constant reference expression OR variable reference expression }
  1168. AS_VMTOFFSET:
  1169. Begin
  1170. if not GotPlus then
  1171. Message(asmr_e_invalid_reference_syntax);
  1172. GotStar:=false;
  1173. GotPlus:=false;
  1174. if (actasmtoken = AS_VMTOFFSET) or
  1175. (SearchIConstant(actasmpattern,l) or
  1176. SearchRecordType(actasmpattern)) then
  1177. begin
  1178. l:=BuildRefConstExpression(negative);
  1179. negative:=false; { "l" was negated if necessary }
  1180. GotPlus:=(prevasmtoken=AS_PLUS);
  1181. GotStar:=(prevasmtoken=AS_STAR);
  1182. case oper.opr.typ of
  1183. OPR_LOCAL :
  1184. begin
  1185. if GotStar then
  1186. Message(asmr_e_invalid_reference_syntax);
  1187. Inc(oper.opr.localsymofs,l);
  1188. end;
  1189. OPR_REFERENCE :
  1190. begin
  1191. if GotStar then
  1192. oper.opr.ref.scalefactor:=l
  1193. else
  1194. Inc(oper.opr.ref.offset,l);
  1195. end;
  1196. end;
  1197. end
  1198. else
  1199. Begin
  1200. if negative and not oper.hasvar then
  1201. Message(asmr_e_only_add_relocatable_symbol)
  1202. else if oper.hasvar and not GotOffset and
  1203. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1204. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1205. HadVar:=oper.hasvar and GotOffset;
  1206. tempstr:=actasmpattern;
  1207. Consume(AS_ID);
  1208. { typecasting? }
  1209. if (actasmtoken=AS_LPAREN) and
  1210. SearchType(tempstr,l) then
  1211. begin
  1212. oper.hastype:=true;
  1213. oper.typesize:=l;
  1214. Consume(AS_LPAREN);
  1215. BuildOperand(oper,true);
  1216. Consume(AS_RPAREN);
  1217. end
  1218. else
  1219. if is_locallabel(tempstr) then
  1220. begin
  1221. CreateLocalLabel(tempstr,hl,false);
  1222. oper.InitRef;
  1223. if not negative then
  1224. begin
  1225. oper.opr.ref.symbol:=hl;
  1226. oper.hasvar:=true;
  1227. end
  1228. else
  1229. oper.opr.ref.relsymbol:=hl;
  1230. {$ifdef i8086}
  1231. if oper.opr.ref.segment=NR_NO then
  1232. oper.opr.ref.segment:=NR_CS;
  1233. {$endif i8086}
  1234. end
  1235. else
  1236. if oper.SetupVar(tempstr,GotOffset) then
  1237. begin
  1238. { force OPR_LOCAL to be a reference }
  1239. if oper.opr.typ=OPR_LOCAL then
  1240. oper.opr.localforceref:=true
  1241. else
  1242. begin
  1243. {$ifdef x86_64}
  1244. if actasmtoken=AS_WRT then
  1245. begin
  1246. if (oper.opr.typ=OPR_REFERENCE) then
  1247. begin
  1248. Consume(AS_WRT);
  1249. Consume(AS___GOTPCREL);
  1250. if (oper.opr.ref.base<>NR_NO) or
  1251. (oper.opr.ref.index<>NR_NO) or
  1252. (oper.opr.ref.offset<>0) then
  1253. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1254. if tf_no_pic_supported in target_info.flags then
  1255. Message(asmr_e_no_gotpcrel_support);
  1256. oper.opr.ref.refaddr:=addr_pic;
  1257. oper.opr.ref.base:=NR_RIP;
  1258. end
  1259. else
  1260. message(asmr_e_invalid_reference_syntax);
  1261. end;
  1262. {$endif x86_64}
  1263. end;
  1264. end
  1265. else
  1266. Message1(sym_e_unknown_id,tempstr);
  1267. { record.field ? }
  1268. if actasmtoken=AS_DOT then
  1269. begin
  1270. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1271. if (hs<>'') then
  1272. Message(asmr_e_invalid_symbol_ref);
  1273. case oper.opr.typ of
  1274. OPR_LOCAL :
  1275. inc(oper.opr.localsymofs,l);
  1276. OPR_REFERENCE :
  1277. inc(oper.opr.ref.offset,l);
  1278. end;
  1279. oper.SetSize(k,false);
  1280. end;
  1281. if GotOffset then
  1282. begin
  1283. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1284. begin
  1285. if (oper.opr.typ=OPR_REFERENCE) then
  1286. oper.opr.ref.base:=NR_NO;
  1287. oper.hasvar:=hadvar;
  1288. end
  1289. else
  1290. begin
  1291. if oper.hasvar and hadvar then
  1292. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1293. { should we allow ?? }
  1294. end;
  1295. end;
  1296. end;
  1297. GotOffset:=false;
  1298. end;
  1299. AS_PLUS :
  1300. Begin
  1301. Consume(AS_PLUS);
  1302. Negative:=false;
  1303. GotPlus:=true;
  1304. GotStar:=false;
  1305. Scale:=0;
  1306. end;
  1307. AS_DOT :
  1308. Begin
  1309. { Handle like a + }
  1310. Consume(AS_DOT);
  1311. Negative:=false;
  1312. GotPlus:=true;
  1313. GotStar:=false;
  1314. Scale:=0;
  1315. end;
  1316. AS_MINUS :
  1317. begin
  1318. Consume(AS_MINUS);
  1319. Negative:=true;
  1320. GotPlus:=true;
  1321. GotStar:=false;
  1322. Scale:=0;
  1323. end;
  1324. AS_STAR : { Scaling, with eax*4 order }
  1325. begin
  1326. Consume(AS_STAR);
  1327. hs:='';
  1328. l:=0;
  1329. case actasmtoken of
  1330. AS_LPAREN :
  1331. l:=BuildConstExpression;
  1332. AS_INTNUM:
  1333. Begin
  1334. hs:=actasmpattern;
  1335. Consume(AS_INTNUM);
  1336. end;
  1337. AS_REGISTER :
  1338. begin
  1339. case oper.opr.typ of
  1340. OPR_REFERENCE :
  1341. begin
  1342. if oper.opr.ref.scalefactor=0 then
  1343. begin
  1344. if scale<>0 then
  1345. begin
  1346. oper.opr.ref.scalefactor:=scale;
  1347. scale:=0;
  1348. end
  1349. else
  1350. Message(asmr_e_wrong_scale_factor);
  1351. end
  1352. else
  1353. Message(asmr_e_invalid_reference_syntax);
  1354. end;
  1355. OPR_LOCAL :
  1356. begin
  1357. if oper.opr.localscale=0 then
  1358. begin
  1359. if scale<>0 then
  1360. begin
  1361. oper.opr.localscale:=scale;
  1362. scale:=0;
  1363. end
  1364. else
  1365. Message(asmr_e_wrong_scale_factor);
  1366. end
  1367. else
  1368. Message(asmr_e_invalid_reference_syntax);
  1369. end;
  1370. end;
  1371. end;
  1372. else
  1373. Message(asmr_e_invalid_reference_syntax);
  1374. end;
  1375. if actasmtoken<>AS_REGISTER then
  1376. begin
  1377. if hs<>'' then
  1378. val(hs,l,code);
  1379. case oper.opr.typ of
  1380. OPR_REFERENCE :
  1381. oper.opr.ref.scalefactor:=l;
  1382. OPR_LOCAL :
  1383. oper.opr.localscale:=l;
  1384. end;
  1385. if l>9 then
  1386. Message(asmr_e_wrong_scale_factor);
  1387. end;
  1388. GotPlus:=false;
  1389. GotStar:=false;
  1390. end;
  1391. AS_REGISTER :
  1392. begin
  1393. if not((GotPlus and (not Negative)) or
  1394. GotStar) then
  1395. Message(asmr_e_invalid_reference_syntax);
  1396. hreg:=actasmregister;
  1397. Consume(AS_REGISTER);
  1398. { this register will be the index:
  1399. 1. just read a *
  1400. 2. next token is a *
  1401. 3. base register is already used }
  1402. case oper.opr.typ of
  1403. OPR_LOCAL :
  1404. begin
  1405. if (oper.opr.localindexreg<>NR_NO) then
  1406. Message(asmr_e_multiple_index);
  1407. {$ifdef x86_64}
  1408. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1409. if (hreg=NR_RIP) then
  1410. Message(asmr_e_no_local_or_para_allowed);
  1411. {$endif x86_64}
  1412. oper.opr.localindexreg:=hreg;
  1413. if scale<>0 then
  1414. begin
  1415. oper.opr.localscale:=scale;
  1416. scale:=0;
  1417. end;
  1418. end;
  1419. OPR_REFERENCE :
  1420. begin
  1421. if (GotStar) or
  1422. (actasmtoken=AS_STAR) or
  1423. (oper.opr.ref.base<>NR_NO) then
  1424. begin
  1425. if (oper.opr.ref.index<>NR_NO) then
  1426. Message(asmr_e_multiple_index);
  1427. oper.opr.ref.index:=hreg;
  1428. if scale<>0 then
  1429. begin
  1430. oper.opr.ref.scalefactor:=scale;
  1431. scale:=0;
  1432. end;
  1433. end
  1434. else
  1435. begin
  1436. oper.opr.ref.base:=hreg;
  1437. {$ifdef x86_64}
  1438. { non-GOT based RIP-relative accesses are also position-independent }
  1439. if (oper.opr.ref.base=NR_RIP) and
  1440. (oper.opr.ref.refaddr<>addr_pic) then
  1441. oper.opr.ref.refaddr:=addr_pic_no_got;
  1442. {$endif x86_64}
  1443. end;
  1444. end;
  1445. end;
  1446. GotPlus:=false;
  1447. GotStar:=false;
  1448. end;
  1449. AS_OFFSET :
  1450. begin
  1451. Consume(AS_OFFSET);
  1452. GotOffset:=true;
  1453. end;
  1454. AS_TYPE,
  1455. AS_NOT,
  1456. AS_STRING,
  1457. AS_INTNUM,
  1458. AS_LPAREN : { Constant reference expression }
  1459. begin
  1460. if not GotPlus and not GotStar then
  1461. Message(asmr_e_invalid_reference_syntax);
  1462. BuildConstSymbolExpression(true,true,GotPlus and negative,l,tempstr,tempsymtyp,isseg,is_farproc_entry);
  1463. { already handled by BuildConstSymbolExpression(); must be
  1464. handled there to avoid [reg-1+1] being interpreted as
  1465. [reg-(1+1)] }
  1466. negative:=false;
  1467. if tempstr<>'' then
  1468. begin
  1469. if GotStar then
  1470. Message(asmr_e_only_add_relocatable_symbol);
  1471. if not assigned(oper.opr.ref.symbol) then
  1472. begin
  1473. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr);
  1474. {$ifdef i8086}
  1475. if isseg then
  1476. begin
  1477. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1478. oper.opr.ref.refaddr:=addr_seg;
  1479. end
  1480. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1481. oper.opr.ref.segment:=NR_CS;
  1482. {$endif i8086}
  1483. end
  1484. else
  1485. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1486. end;
  1487. case oper.opr.typ of
  1488. OPR_REFERENCE :
  1489. begin
  1490. if GotStar then
  1491. oper.opr.ref.scalefactor:=l
  1492. else if (prevasmtoken = AS_STAR) then
  1493. begin
  1494. if scale<>0 then
  1495. scale:=l*scale
  1496. else
  1497. scale:=l;
  1498. end
  1499. else
  1500. begin
  1501. Inc(oper.opr.ref.offset,l);
  1502. Inc(oper.opr.constoffset,l);
  1503. end;
  1504. end;
  1505. OPR_LOCAL :
  1506. begin
  1507. if GotStar then
  1508. oper.opr.localscale:=l
  1509. else if (prevasmtoken = AS_STAR) then
  1510. begin
  1511. if scale<>0 then
  1512. scale:=l*scale
  1513. else
  1514. scale:=l;
  1515. end
  1516. else
  1517. Inc(oper.opr.localsymofs,l);
  1518. end;
  1519. end;
  1520. GotPlus:=(prevasmtoken=AS_PLUS) or
  1521. (prevasmtoken=AS_MINUS);
  1522. if GotPlus then
  1523. negative := prevasmtoken = AS_MINUS;
  1524. GotStar:=(prevasmtoken=AS_STAR);
  1525. end;
  1526. AS_RBRACKET :
  1527. begin
  1528. if GotPlus or GotStar then
  1529. Message(asmr_e_invalid_reference_syntax);
  1530. Consume(AS_RBRACKET);
  1531. break;
  1532. end;
  1533. else
  1534. Begin
  1535. Message(asmr_e_invalid_reference_syntax);
  1536. RecoverConsume(true);
  1537. break;
  1538. end;
  1539. end;
  1540. until false;
  1541. end;
  1542. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1543. var
  1544. l : aint;
  1545. tempstr : string;
  1546. tempsymtyp : tasmsymtype;
  1547. isseg: boolean;
  1548. is_farproc_entry : boolean;
  1549. begin
  1550. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1551. Message(asmr_e_invalid_operand_type);
  1552. BuildConstSymbolExpression(true,false,false,l,tempstr,tempsymtyp,isseg,is_farproc_entry);
  1553. {$ifdef i8086}
  1554. if tempstr='@DATA' then
  1555. begin
  1556. if not isseg then
  1557. Message(asmr_e_CODE_or_DATA_without_SEG);
  1558. oper.SetupData;
  1559. end
  1560. else if tempstr='@CODE' then
  1561. begin
  1562. if not isseg then
  1563. Message(asmr_e_CODE_or_DATA_without_SEG);
  1564. oper.SetupCode;
  1565. end
  1566. else
  1567. {$endif i8086}
  1568. if tempstr<>'' then
  1569. begin
  1570. oper.opr.typ:=OPR_SYMBOL;
  1571. oper.opr.symofs:=l;
  1572. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr);
  1573. oper.opr.symseg:=isseg;
  1574. oper.opr.sym_farproc_entry:=is_farproc_entry;
  1575. end
  1576. else
  1577. if oper.opr.typ=OPR_NONE then
  1578. begin
  1579. oper.opr.typ:=OPR_CONSTANT;
  1580. oper.opr.val:=l;
  1581. end
  1582. else
  1583. inc(oper.opr.val,l);
  1584. end;
  1585. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1586. procedure AddLabelOperand(hl:tasmlabel);
  1587. begin
  1588. if (oper.opr.typ=OPR_NONE) and
  1589. is_calljmp(actopcode) then
  1590. begin
  1591. oper.opr.typ:=OPR_SYMBOL;
  1592. oper.opr.symbol:=hl;
  1593. end
  1594. else
  1595. begin
  1596. oper.InitRef;
  1597. oper.opr.ref.symbol:=hl;
  1598. {$ifdef i8086}
  1599. if oper.opr.ref.segment=NR_NO then
  1600. oper.opr.ref.segment:=NR_CS;
  1601. {$endif i8086}
  1602. end;
  1603. end;
  1604. var
  1605. expr,
  1606. hs : string;
  1607. tempreg : tregister;
  1608. l : aint;
  1609. hl : tasmlabel;
  1610. toffset,
  1611. tsize : aint;
  1612. begin
  1613. expr:='';
  1614. repeat
  1615. if actasmtoken=AS_DOT then
  1616. begin
  1617. if expr<>'' then
  1618. begin
  1619. BuildRecordOffsetSize(expr,toffset,tsize,hs,false);
  1620. if (oper.opr.typ<>OPR_NONE) and
  1621. (hs<>'') then
  1622. Message(asmr_e_wrong_sym_type);
  1623. oper.SetSize(tsize,true);
  1624. { we have used the size of a field. Reset the typesize of the record }
  1625. oper.typesize:=0;
  1626. case oper.opr.typ of
  1627. OPR_LOCAL :
  1628. begin
  1629. { don't allow direct access to fields of parameters, because that
  1630. will generate buggy code. Allow it only for explicit typecasting
  1631. and when the parameter is in a register (delphi compatible) }
  1632. if (not oper.hastype) and
  1633. (oper.opr.localsym.owner.symtabletype=parasymtable) and
  1634. (current_procinfo.procdef.proccalloption<>pocall_register) then
  1635. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1636. inc(oper.opr.localsymofs,toffset);
  1637. oper.opr.localvarsize := tsize;
  1638. end;
  1639. OPR_CONSTANT :
  1640. inc(oper.opr.val,toffset);
  1641. OPR_REFERENCE :
  1642. begin
  1643. inc(oper.opr.ref.offset,toffset);
  1644. oper.opr.varsize := tsize;
  1645. end;
  1646. OPR_NONE :
  1647. begin
  1648. if (hs <> '') then
  1649. begin
  1650. oper.opr.typ:=OPR_SYMBOL;
  1651. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs);
  1652. end
  1653. else
  1654. begin
  1655. oper.opr.typ:=OPR_CONSTANT;
  1656. oper.opr.val:=toffset;
  1657. end;
  1658. end;
  1659. OPR_REGISTER :
  1660. Message(asmr_e_invalid_reference_syntax);
  1661. OPR_SYMBOL:
  1662. Message(asmr_e_invalid_symbol_ref);
  1663. else
  1664. internalerror(200309222);
  1665. end;
  1666. expr:='';
  1667. end
  1668. else
  1669. begin
  1670. { See it as a separator }
  1671. Consume(AS_DOT);
  1672. end;
  1673. end;
  1674. case actasmtoken of
  1675. {$ifndef i8086}
  1676. AS_SEG :
  1677. Begin
  1678. Message(asmr_e_seg_not_supported);
  1679. Consume(actasmtoken);
  1680. end;
  1681. {$else not i8086}
  1682. AS_SEG,
  1683. {$endif not i8086}
  1684. AS_OFFSET,
  1685. AS_SIZEOF,
  1686. AS_VMTOFFSET,
  1687. AS_TYPE,
  1688. AS_NOT,
  1689. AS_STRING,
  1690. AS_PLUS,
  1691. AS_MINUS,
  1692. AS_LPAREN,
  1693. AS_INTNUM :
  1694. begin
  1695. case oper.opr.typ of
  1696. OPR_REFERENCE :
  1697. {$ifndef x86_64}
  1698. { this is for the i386 scenario where you have
  1699. <load got into ebx>
  1700. mov eax, [ebx].offset globalvar
  1701. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  1702. relative accesses)
  1703. }
  1704. if (actasmtoken=AS_OFFSET) and
  1705. (cs_create_pic in current_settings.moduleswitches) then
  1706. begin
  1707. Consume(AS_OFFSET);
  1708. oper.opr.ref.refaddr:=addr_pic;
  1709. BuildOperand(oper,false);
  1710. end
  1711. else
  1712. {$endif x86_64}
  1713. begin
  1714. l := BuildRefConstExpression;
  1715. inc(oper.opr.ref.offset,l);
  1716. inc(oper.opr.constoffset,l);
  1717. end;
  1718. OPR_LOCAL :
  1719. begin
  1720. l := BuildConstExpression;
  1721. inc(oper.opr.localsymofs,l);
  1722. inc(oper.opr.localconstoffset,l);
  1723. end;
  1724. OPR_NONE,
  1725. OPR_CONSTANT :
  1726. BuildConstantOperand(oper);
  1727. else
  1728. Message(asmr_e_invalid_operand_type);
  1729. end;
  1730. end;
  1731. AS_PTR :
  1732. begin
  1733. if not oper.hastype then
  1734. begin
  1735. if (oper.opr.typ=OPR_CONSTANT) then
  1736. begin
  1737. oper.typesize:=oper.opr.val;
  1738. { reset constant value of operand }
  1739. oper.opr.typ:=OPR_NONE;
  1740. oper.opr.val:=0;
  1741. end
  1742. else
  1743. Message(asmr_e_syn_operand);
  1744. end;
  1745. Consume(AS_PTR);
  1746. oper.InitRef;
  1747. { if the operand subscripts a record, the typesize will be
  1748. rest -> save it here and restore it afterwards }
  1749. l:=oper.typesize;
  1750. BuildOperand(oper,false);
  1751. oper.setsize(l,true);
  1752. end;
  1753. AS_ID : { A constant expression, or a Variable ref. }
  1754. Begin
  1755. { Label or Special symbol reference? }
  1756. if actasmpattern[1] = '@' then
  1757. Begin
  1758. if actasmpattern = '@RESULT' then
  1759. Begin
  1760. oper.SetupResult;
  1761. Consume(AS_ID);
  1762. end
  1763. else
  1764. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1765. begin
  1766. {$ifdef i8086}
  1767. Message(asmr_e_CODE_or_DATA_without_SEG);
  1768. {$else i8086}
  1769. Message(asmr_w_CODE_and_DATA_not_supported);
  1770. {$endif i8086}
  1771. Consume(AS_ID);
  1772. end
  1773. else
  1774. { Local Label }
  1775. begin
  1776. CreateLocalLabel(actasmpattern,hl,false);
  1777. Consume(AS_ID);
  1778. AddLabelOperand(hl);
  1779. end;
  1780. end
  1781. else
  1782. { support result for delphi modes }
  1783. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  1784. begin
  1785. oper.SetUpResult;
  1786. Consume(AS_ID);
  1787. end
  1788. { probably a variable or normal expression }
  1789. { or a procedure (such as in CALL ID) }
  1790. else
  1791. Begin
  1792. { is it a constant ? }
  1793. if SearchIConstant(actasmpattern,l) then
  1794. Begin
  1795. case oper.opr.typ of
  1796. OPR_REFERENCE :
  1797. begin
  1798. l := BuildRefConstExpression;
  1799. inc(oper.opr.ref.offset,l);
  1800. inc(oper.opr.constoffset,l);
  1801. end;
  1802. OPR_LOCAL :
  1803. begin
  1804. l := BuildRefConstExpression;
  1805. inc(oper.opr.localsymofs,l);
  1806. inc(oper.opr.localconstoffset,l);
  1807. end;
  1808. OPR_NONE,
  1809. OPR_CONSTANT :
  1810. BuildConstantOperand(oper);
  1811. else
  1812. Message(asmr_e_invalid_operand_type);
  1813. end;
  1814. end
  1815. else
  1816. { Check for pascal label }
  1817. if SearchLabel(actasmpattern,hl,false) then
  1818. begin
  1819. Consume(AS_ID);
  1820. AddLabelOperand(hl);
  1821. end
  1822. else
  1823. { is it a normal variable ? }
  1824. Begin
  1825. expr:=actasmpattern;
  1826. Consume(AS_ID);
  1827. { typecasting? }
  1828. if SearchType(expr,l) then
  1829. begin
  1830. oper.hastype:=true;
  1831. oper.typesize:=l;
  1832. case actasmtoken of
  1833. AS_LPAREN :
  1834. begin
  1835. { Support Type([Reference]) }
  1836. Consume(AS_LPAREN);
  1837. BuildOperand(oper,true);
  1838. { Delphi also supports Type(Register) and
  1839. interprets it the same as Type([Register]). }
  1840. if (oper.opr.typ = OPR_REGISTER) then
  1841. { This also sets base to the register. }
  1842. oper.InitRef;
  1843. Consume(AS_RPAREN);
  1844. end;
  1845. AS_LBRACKET :
  1846. begin
  1847. { Support Var.Type[Index] }
  1848. { Convert @label.Byte[1] to reference }
  1849. if oper.opr.typ=OPR_SYMBOL then
  1850. oper.initref;
  1851. end;
  1852. end;
  1853. end
  1854. else
  1855. begin
  1856. if not oper.SetupVar(expr,false) then
  1857. Begin
  1858. { not a variable, check special variables.. }
  1859. if expr = 'SELF' then
  1860. oper.SetupSelf
  1861. else
  1862. Message1(sym_e_unknown_id,expr);
  1863. expr:='';
  1864. end;
  1865. { indexed access to variable? }
  1866. if actasmtoken=AS_LBRACKET then
  1867. begin
  1868. { ... then the operand size is not known anymore }
  1869. oper.size:=OS_NO;
  1870. BuildReference(oper);
  1871. end;
  1872. end;
  1873. end;
  1874. end;
  1875. end;
  1876. AS_REGISTER : { Register, a variable reference or a constant reference }
  1877. begin
  1878. { save the type of register used. }
  1879. tempreg:=actasmregister;
  1880. Consume(AS_REGISTER);
  1881. if actasmtoken = AS_COLON then
  1882. Begin
  1883. Consume(AS_COLON);
  1884. oper.InitRef;
  1885. oper.opr.ref.segment:=tempreg;
  1886. BuildReference(oper);
  1887. end
  1888. else
  1889. { Simple register }
  1890. begin
  1891. if (oper.opr.typ <> OPR_NONE) then
  1892. Message(asmr_e_syn_operand);
  1893. oper.opr.typ:=OPR_REGISTER;
  1894. oper.opr.reg:=tempreg;
  1895. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1896. end;
  1897. end;
  1898. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1899. Begin
  1900. BuildReference(oper);
  1901. end;
  1902. AS_DWORD,
  1903. AS_BYTE,
  1904. AS_WORD,
  1905. AS_TBYTE,
  1906. AS_DQWORD,
  1907. AS_QWORD,
  1908. AS_OWORD,
  1909. AS_XMMWORD,
  1910. AS_YWORD,
  1911. AS_YMMWORD:
  1912. begin
  1913. { Type specifier }
  1914. oper.hastype:=true;
  1915. oper.typesize:=0;
  1916. case actasmtoken of
  1917. AS_DWORD : oper.typesize:=4;
  1918. AS_WORD : oper.typesize:=2;
  1919. AS_BYTE : oper.typesize:=1;
  1920. AS_QWORD : oper.typesize:=8;
  1921. AS_DQWORD : oper.typesize:=16;
  1922. AS_TBYTE : oper.typesize:=10;
  1923. AS_OWORD,
  1924. AS_XMMWORD: oper.typesize:=16;
  1925. AS_YWORD,
  1926. AS_YMMWORD: oper.typesize:=32;
  1927. else
  1928. internalerror(2010061101);
  1929. end;
  1930. Consume(actasmtoken);
  1931. if (actasmtoken=AS_LPAREN) then
  1932. begin
  1933. { Support "xxx ptr [Reference]" }
  1934. { in case the expression subscripts a record, the typesize
  1935. is reset, so save the explicit size we set above }
  1936. l:=oper.typesize;
  1937. Consume(AS_LPAREN);
  1938. BuildOperand(oper,true);
  1939. Consume(AS_RPAREN);
  1940. oper.setsize(l,true);
  1941. end;
  1942. end;
  1943. AS_SEPARATOR,
  1944. AS_END,
  1945. AS_COMMA,
  1946. AS_COLON:
  1947. begin
  1948. break;
  1949. end;
  1950. AS_RPAREN:
  1951. begin
  1952. if not istypecast then
  1953. begin
  1954. Message(asmr_e_syn_operand);
  1955. Consume(AS_RPAREN);
  1956. end
  1957. else
  1958. break;
  1959. end;
  1960. else
  1961. begin
  1962. Message(asmr_e_syn_operand);
  1963. RecoverConsume(true);
  1964. break;
  1965. end;
  1966. end;
  1967. until false;
  1968. { End of operand, update size if a typecast is forced }
  1969. if (oper.typesize<>0) and
  1970. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  1971. oper.SetSize(oper.typesize,true);
  1972. {$ifdef i8086}
  1973. { references to a procedure/function entry, without an explicit segment
  1974. override, are added an CS: override by default (this is Turbo Pascal 7
  1975. compatible) }
  1976. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  1977. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1978. oper.opr.ref.segment:=NR_CS;
  1979. {$endif i8086}
  1980. end;
  1981. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  1982. var
  1983. PrefixOp,OverrideOp: tasmop;
  1984. operandnum : longint;
  1985. t: TRegister;
  1986. is_far_const:boolean;
  1987. i:byte;
  1988. tmp: toperand;
  1989. {$ifdef i8086}
  1990. hsymbol: TAsmSymbol;
  1991. hoffset: ASizeInt;
  1992. href_farproc_entry: Boolean;
  1993. {$endif i8086}
  1994. begin
  1995. PrefixOp:=A_None;
  1996. OverrideOp:=A_None;
  1997. is_far_const:=false;
  1998. { prefix seg opcode / prefix opcode }
  1999. repeat
  2000. if is_prefix(actopcode) then
  2001. with instr do
  2002. begin
  2003. PrefixOp:=ActOpcode;
  2004. opcode:=ActOpcode;
  2005. condition:=ActCondition;
  2006. opsize:=ActOpsize;
  2007. ConcatInstruction(curlist);
  2008. consume(AS_OPCODE);
  2009. end
  2010. else
  2011. if is_override(actopcode) then
  2012. with instr do
  2013. begin
  2014. OverrideOp:=ActOpcode;
  2015. opcode:=ActOpcode;
  2016. condition:=ActCondition;
  2017. opsize:=ActOpsize;
  2018. ConcatInstruction(curlist);
  2019. consume(AS_OPCODE);
  2020. end
  2021. else
  2022. break;
  2023. { allow for newline after prefix or override }
  2024. while actasmtoken=AS_SEPARATOR do
  2025. consume(AS_SEPARATOR);
  2026. until (actasmtoken<>AS_OPCODE);
  2027. { opcode }
  2028. if (actasmtoken <> AS_OPCODE) then
  2029. begin
  2030. Message(asmr_e_invalid_or_missing_opcode);
  2031. RecoverConsume(false);
  2032. exit;
  2033. end;
  2034. { Fill the instr object with the current state }
  2035. with instr do
  2036. begin
  2037. Opcode:=ActOpcode;
  2038. condition:=ActCondition;
  2039. opsize:=ActOpsize;
  2040. { Valid combination of prefix/override and instruction ? }
  2041. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2042. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2043. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2044. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2045. end;
  2046. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2047. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2048. these instructions doesn't change anything in the internal assember,
  2049. so change the opcode }
  2050. if (instr.opcode=A_POPF) then
  2051. instr.opcode:=A_POPFW
  2052. else if (instr.opcode=A_PUSHF) then
  2053. instr.opcode:=A_PUSHFW
  2054. {$ifndef x86_64}
  2055. else if (instr.opcode=A_PUSHA) then
  2056. instr.opcode:=A_PUSHAW
  2057. else if (instr.opcode=A_POPA) then
  2058. instr.opcode:=A_POPAW
  2059. {$endif x86_64}
  2060. {$ifdef i8086}
  2061. { ret is converted to retn or retf, depending on the call model of the
  2062. current procedure (BP7 compatible) }
  2063. else if (instr.opcode=A_RET) then
  2064. begin
  2065. if is_proc_far(current_procinfo.procdef) and
  2066. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2067. instr.opcode:=A_RETF
  2068. else
  2069. instr.opcode:=A_RETN;
  2070. end
  2071. {$endif i8086}
  2072. ;
  2073. { We are reading operands, so opcode will be an AS_ID }
  2074. { process operands backwards to get them in AT&T order }
  2075. operandnum:=max_operands;
  2076. is_far_const:=false;
  2077. Consume(AS_OPCODE);
  2078. { Zero operand opcode ? }
  2079. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2080. exit;
  2081. { Read Operands }
  2082. repeat
  2083. case actasmtoken of
  2084. { End of asm operands for this opcode }
  2085. AS_END,
  2086. AS_SEPARATOR :
  2087. break;
  2088. { Operand delimiter }
  2089. AS_COMMA :
  2090. begin
  2091. { should have something before the comma }
  2092. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2093. Message(asmr_e_syntax_error);
  2094. if operandnum <= 1 then
  2095. Message(asmr_e_too_many_operands)
  2096. else
  2097. Dec(operandnum);
  2098. Consume(AS_COMMA);
  2099. end;
  2100. {Far constant, i.e. jmp $0000:$11111111.}
  2101. AS_COLON:
  2102. begin
  2103. is_far_const:=true;
  2104. if operandnum<max_operands then
  2105. message(asmr_e_too_many_operands)
  2106. else
  2107. dec(operandnum);
  2108. consume(AS_COLON);
  2109. end;
  2110. { Type specifier }
  2111. AS_NEAR,
  2112. AS_FAR :
  2113. begin
  2114. if actasmtoken = AS_NEAR then
  2115. begin
  2116. {$ifndef i8086}
  2117. Message(asmr_w_near_ignored);
  2118. {$endif not i8086}
  2119. instr.opsize:=S_NEAR;
  2120. end
  2121. else
  2122. begin
  2123. {$ifndef i8086}
  2124. Message(asmr_w_far_ignored);
  2125. {$endif not i8086}
  2126. instr.opsize:=S_FAR;
  2127. end;
  2128. Consume(actasmtoken);
  2129. if actasmtoken=AS_PTR then
  2130. begin
  2131. Consume(AS_PTR);
  2132. instr.Operands[operandnum].InitRef;
  2133. end;
  2134. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2135. end;
  2136. else
  2137. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2138. end; { end case }
  2139. until false;
  2140. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2141. for i:=operandnum to max_operands do
  2142. begin
  2143. tmp:=instr.operands[i+1-operandnum];
  2144. instr.operands[i+1-operandnum]:=instr.operands[i];
  2145. instr.operands[i]:=tmp;
  2146. end;
  2147. operandnum:=(max_operands+1)-operandnum;
  2148. instr.ops:=operandnum;
  2149. { Check operands }
  2150. for i:=1 to operandnum do
  2151. begin
  2152. if is_far_const and
  2153. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2154. message(asmr_e_expr_illegal)
  2155. else
  2156. if instr.operands[i].opr.typ=OPR_NONE then
  2157. Message(asmr_e_syntax_error);
  2158. end;
  2159. { e.g. for "push dword 1", "push word 6" }
  2160. if (instr.ops=1) and
  2161. (instr.operands[1].typesize<>0) then
  2162. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2163. {$ifdef i8086}
  2164. for i:=1 to operandnum do
  2165. with instr.operands[i].opr do
  2166. begin
  2167. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2168. but Turbo Pascal 7 compatible. }
  2169. if (instr.opcode in [A_CALL,A_JMP]) and (typ=OPR_REFERENCE) and
  2170. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2171. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2172. begin
  2173. hsymbol:=ref.symbol;
  2174. hoffset:=ref.offset;
  2175. href_farproc_entry:=ref_farproc_entry;
  2176. typ:=OPR_SYMBOL;
  2177. symbol:=hsymbol;
  2178. symofs:=hoffset;
  2179. symseg:=False;
  2180. sym_farproc_entry:=href_farproc_entry;
  2181. end;
  2182. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2183. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2184. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2185. instr.opsize:=S_FAR;
  2186. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2187. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2188. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2189. instr.opsize:=S_FAR;
  2190. end;
  2191. {$endif i8086}
  2192. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2193. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2194. begin
  2195. for i:=1 to operandnum do
  2196. begin
  2197. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2198. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2199. (instr.operands[i].opr.ref.index = NR_NO) then
  2200. begin
  2201. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2202. instr.operands[i].opr.ref.base := NR_NO;
  2203. end
  2204. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2205. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2206. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2207. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2208. begin
  2209. // exchange base- and index-register
  2210. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2211. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2212. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2213. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2214. else
  2215. begin
  2216. t := instr.operands[i].opr.ref.base;
  2217. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2218. instr.operands[i].opr.ref.index := t;
  2219. end;
  2220. end;
  2221. end;
  2222. end;
  2223. end;
  2224. Procedure tx86intreader.BuildConstant(constsize: byte);
  2225. var
  2226. asmsymtyp : tasmsymtype;
  2227. asmsym,
  2228. expr: string;
  2229. value : aint;
  2230. isseg: boolean;
  2231. is_farproc_entry : boolean;
  2232. Begin
  2233. Repeat
  2234. Case actasmtoken of
  2235. AS_STRING:
  2236. Begin
  2237. { DD and DW cases }
  2238. if constsize <> 1 then
  2239. Begin
  2240. if Not PadZero(actasmpattern,constsize) then
  2241. Message(scan_f_string_exceeds_line);
  2242. end;
  2243. expr:=actasmpattern;
  2244. Consume(AS_STRING);
  2245. Case actasmtoken of
  2246. AS_COMMA:
  2247. Consume(AS_COMMA);
  2248. AS_END,
  2249. AS_SEPARATOR: ;
  2250. else
  2251. Message(asmr_e_invalid_string_expression);
  2252. end;
  2253. ConcatString(curlist,expr);
  2254. end;
  2255. AS_PLUS,
  2256. AS_MINUS,
  2257. AS_LPAREN,
  2258. AS_NOT,
  2259. AS_INTNUM,
  2260. {$ifdef i8086}
  2261. AS_SEG,
  2262. {$endif i8086}
  2263. AS_ID :
  2264. Begin
  2265. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp,isseg,is_farproc_entry);
  2266. if asmsym<>'' then
  2267. begin
  2268. if (not isseg) and (constsize<>sizeof(pint)) then
  2269. Message1(asmr_w_const32bit_for_address,asmsym);
  2270. {$ifdef i8086}
  2271. if asmsym='@DATA' then
  2272. begin
  2273. if not isseg then
  2274. Message(asmr_e_CODE_or_DATA_without_SEG);
  2275. if constsize<2 then
  2276. Message1(asmr_e_const16bit_for_segment,asmsym);
  2277. if current_settings.x86memorymodel=mm_huge then
  2278. curlist.concat(Tai_const.Create_fardataseg)
  2279. else
  2280. curlist.concat(Tai_const.Create_dgroup);
  2281. if constsize>2 then
  2282. ConcatConstant(curlist,0,constsize-2);
  2283. end
  2284. else if asmsym='@CODE' then
  2285. begin
  2286. if not isseg then
  2287. Message(asmr_e_CODE_or_DATA_without_SEG);
  2288. if constsize<2 then
  2289. Message1(asmr_e_const16bit_for_segment,asmsym);
  2290. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2291. if constsize>2 then
  2292. ConcatConstant(curlist,0,constsize-2);
  2293. end
  2294. else if isseg then
  2295. begin
  2296. if constsize<2 then
  2297. Message1(asmr_e_const16bit_for_segment,asmsym);
  2298. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2299. if constsize>2 then
  2300. ConcatConstant(curlist,0,constsize-2);
  2301. end
  2302. else
  2303. {$endif i8086}
  2304. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value);
  2305. end
  2306. else
  2307. ConcatConstant(curlist,value,constsize);
  2308. end;
  2309. AS_COMMA:
  2310. begin
  2311. Consume(AS_COMMA);
  2312. end;
  2313. AS_ALIGN,
  2314. AS_DB,
  2315. AS_DW,
  2316. AS_DD,
  2317. AS_DQ,
  2318. AS_OPCODE,
  2319. AS_END,
  2320. AS_SEPARATOR:
  2321. break;
  2322. else
  2323. begin
  2324. Message(asmr_e_syn_constant);
  2325. RecoverConsume(false);
  2326. end
  2327. end;
  2328. Until false;
  2329. end;
  2330. function tx86intreader.Assemble: tlinkedlist;
  2331. Var
  2332. hl : tasmlabel;
  2333. instr : Tx86Instruction;
  2334. Begin
  2335. Message1(asmr_d_start_reading,'intel');
  2336. inexpression:=FALSE;
  2337. firsttoken:=TRUE;
  2338. { sets up all opcode and register tables in uppercase
  2339. done in the construtor now
  2340. if not _asmsorted then
  2341. Begin
  2342. SetupTables;
  2343. _asmsorted:=TRUE;
  2344. end;
  2345. }
  2346. curlist:=TAsmList.Create;
  2347. { we might need to know which parameters are passed in registers }
  2348. if not parse_generic then
  2349. current_procinfo.generate_parameter_info;
  2350. { start tokenizer }
  2351. gettoken;
  2352. { main loop }
  2353. repeat
  2354. case actasmtoken of
  2355. AS_LLABEL:
  2356. Begin
  2357. if CreateLocalLabel(actasmpattern,hl,true) then
  2358. ConcatLabel(curlist,hl);
  2359. Consume(AS_LLABEL);
  2360. end;
  2361. AS_LABEL:
  2362. Begin
  2363. if SearchLabel(upper(actasmpattern),hl,true) then
  2364. ConcatLabel(curlist,hl)
  2365. else
  2366. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2367. Consume(AS_LABEL);
  2368. end;
  2369. AS_DW :
  2370. Begin
  2371. inexpression:=true;
  2372. Consume(AS_DW);
  2373. BuildConstant(2);
  2374. inexpression:=false;
  2375. end;
  2376. AS_DB :
  2377. Begin
  2378. inexpression:=true;
  2379. Consume(AS_DB);
  2380. BuildConstant(1);
  2381. inexpression:=false;
  2382. end;
  2383. AS_DD :
  2384. Begin
  2385. inexpression:=true;
  2386. Consume(AS_DD);
  2387. BuildConstant(4);
  2388. inexpression:=false;
  2389. end;
  2390. {$ifdef cpu64bitaddr}
  2391. AS_DQ:
  2392. Begin
  2393. inexpression:=true;
  2394. Consume(AS_DQ);
  2395. BuildConstant(8);
  2396. inexpression:=false;
  2397. end;
  2398. {$endif cpu64bitaddr}
  2399. AS_ALIGN:
  2400. Begin
  2401. Consume(AS_ALIGN);
  2402. ConcatAlign(curlist,BuildConstExpression);
  2403. if actasmtoken<>AS_SEPARATOR then
  2404. Consume(AS_SEPARATOR);
  2405. end;
  2406. AS_OPCODE :
  2407. Begin
  2408. instr:=Tx86Instruction.Create(Tx86Operand);
  2409. BuildOpcode(instr);
  2410. with instr do
  2411. begin
  2412. CheckNonCommutativeOpcodes;
  2413. AddReferenceSizes;
  2414. SetInstructionOpsize;
  2415. CheckOperandSizes;
  2416. ConcatInstruction(curlist);
  2417. end;
  2418. instr.Free;
  2419. end;
  2420. AS_SEPARATOR :
  2421. Begin
  2422. Consume(AS_SEPARATOR);
  2423. end;
  2424. AS_END :
  2425. break; { end assembly block }
  2426. else
  2427. Begin
  2428. Message(asmr_e_syntax_error);
  2429. RecoverConsume(false);
  2430. end;
  2431. end; { end case }
  2432. until false;
  2433. { check that all referenced local labels are defined }
  2434. checklocallabels;
  2435. { Return the list in an asmnode }
  2436. assemble:=curlist;
  2437. Message1(asmr_d_finish_reading,'intel');
  2438. end;
  2439. end.