2
0

rautils.pas 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. This unit implements some support routines for assembler parsing
  4. independent of the processor
  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. Unit RAUtils;
  18. {$i fpcdefs.inc}
  19. Interface
  20. Uses
  21. cutils,cclasses,
  22. globtype,aasmbase,aasmtai,aasmdata,cpubase,cpuinfo,cgbase,cgutils,
  23. symconst,symbase,symtype,symdef,symsym,constexp,symcpu;
  24. Const
  25. RPNMax = 10; { I think you only need 4, but just to be safe }
  26. OpMax = 25;
  27. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  28. {---------------------------------------------------------------------
  29. Instruction management
  30. ---------------------------------------------------------------------}
  31. type
  32. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  33. OPR_REFERENCE,OPR_REGISTER,OPR_COND,OPR_REGSET,
  34. OPR_SHIFTEROP,OPR_MODEFLAGS,OPR_SPECIALREG,
  35. OPR_REGPAIR);
  36. TOprRec = record
  37. case typ:TOprType of
  38. OPR_NONE : ();
  39. {$if defined(AVR)}
  40. OPR_CONSTANT : (val:longint);
  41. {$elseif defined(i8086)}
  42. OPR_CONSTANT : (val:longint);
  43. {$else}
  44. OPR_CONSTANT : (val:aint);
  45. {$endif}
  46. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint;symseg:boolean;sym_farproc_entry:boolean);
  47. OPR_REFERENCE : (varsize:asizeint; constoffset: asizeint;ref_farproc_entry:boolean;ref:treference);
  48. OPR_LOCAL : (localvarsize, localconstoffset: asizeint;localsym:tabstractnormalvarsym;localsymofs:aint;localsegment,localindexreg:tregister;localscale:byte;localgetoffset,localforceref:boolean);
  49. OPR_REGISTER : (reg:tregister);
  50. {$ifdef m68k}
  51. OPR_REGSET : (regsetdata,regsetaddr,regsetfpu : tcpuregisterset);
  52. OPR_REGPAIR : (reghi,reglo: tregister);
  53. {$endif m68k}
  54. {$ifdef powerpc}
  55. OPR_COND : (cond : tasmcond);
  56. {$endif powerpc}
  57. {$ifdef POWERPC64}
  58. OPR_COND : (cond : tasmcond);
  59. {$endif POWERPC64}
  60. {$ifdef arm}
  61. OPR_REGSET : (regset : tcpuregisterset; regtype: tregistertype; subreg: tsubregister; usermode: boolean);
  62. OPR_SHIFTEROP : (shifterop : tshifterop);
  63. OPR_COND : (cc : tasmcond);
  64. OPR_MODEFLAGS : (flags : tcpumodeflags);
  65. OPR_SPECIALREG: (specialreg : tregister; specialregflags : tspecialregflags);
  66. {$endif arm}
  67. {$ifdef aarch64}
  68. OPR_SHIFTEROP : (shifterop : tshifterop);
  69. OPR_COND : (cc : tasmcond);
  70. {$endif aarch64}
  71. end;
  72. TOperand = class
  73. opr : TOprRec;
  74. typesize : byte;
  75. haslabelref, { if the operand has a label, used in a reference like a
  76. var (e.g. 'mov ax, word ptr [label+5]', but *not*
  77. e.g. 'jmp label') }
  78. hasproc, { if the operand has a procedure/function reference }
  79. hastype, { if the operand has typecasted variable }
  80. hasvar : boolean; { if the operand is loaded with a variable }
  81. size : TCGSize;
  82. constructor create;virtual;
  83. destructor destroy;override;
  84. Procedure SetSize(_size:longint;force:boolean);virtual;
  85. Procedure SetCorrectSize(opcode:tasmop);virtual;
  86. Function SetupResult:boolean;virtual;
  87. Function SetupSelf:boolean;
  88. Function SetupOldEBP:boolean;
  89. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  90. Function CheckOperand: boolean; virtual;
  91. Procedure InitRef;
  92. Procedure InitRefConvertLocal;
  93. protected
  94. Procedure InitRefError;
  95. end;
  96. TCOperand = class of TOperand;
  97. TInstruction = class
  98. operands : array[1..max_operands] of toperand;
  99. opcode : tasmop;
  100. condition : tasmcond;
  101. ops : byte;
  102. labeled : boolean;
  103. filepos : tfileposinfo;
  104. constructor create(optype : tcoperand);virtual;
  105. destructor destroy;override;
  106. { converts the instruction to an instruction how it's used by the assembler writer
  107. and concats it to the passed list. The newly created item is returned if the
  108. instruction was valid, otherwise nil is returned }
  109. function ConcatInstruction(p:TAsmList) : tai;virtual;
  110. end;
  111. {---------------------------------------------------------------------}
  112. { Expression parser types }
  113. {---------------------------------------------------------------------}
  114. TExprOperator = record
  115. ch: char; { operator }
  116. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  117. end;
  118. {**********************************************************************}
  119. { The following operators are supported: }
  120. { '+' : addition }
  121. { '-' : subtraction }
  122. { '*' : multiplication }
  123. { '/' : modulo division }
  124. { '^' : exclusive or }
  125. { '<' : shift left }
  126. { '>' : shift right }
  127. { '&' : bitwise and }
  128. { '|' : bitwise or }
  129. { '~' : bitwise complement }
  130. { '%' : modulo division }
  131. { nnn: longint numbers }
  132. { ( and ) parenthesis }
  133. { [ and ] another kind of parenthesis }
  134. {**********************************************************************}
  135. TExprParse = class
  136. public
  137. Constructor create;
  138. Destructor Destroy;override;
  139. Function Evaluate(Expr: String): tcgint;
  140. Function Priority(_Operator: Char): aint;
  141. private
  142. RPNStack : Array[1..RPNMax] of tcgint; { Stack For RPN calculator }
  143. RPNTop : tcgint;
  144. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  145. OpTop : tcgint;
  146. Procedure RPNPush(Num: tcgint);
  147. Function RPNPop: tcgint;
  148. Procedure RPNCalc(const token: String; prefix: boolean);
  149. Procedure OpPush(_Operator: char; prefix: boolean);
  150. { In reality returns TExprOperaotr }
  151. Procedure OpPop(var _Operator:TExprOperator);
  152. end;
  153. { Evaluate an expression string to a tcgint }
  154. Function CalculateExpression(const expression: string): tcgint;
  155. {---------------------------------------------------------------------}
  156. { String routines }
  157. {---------------------------------------------------------------------}
  158. Function ParseVal(const S:String;base:byte):tcgint;
  159. Function PadZero(Var s: String; n: byte): Boolean;
  160. Function EscapeToPascal(const s:string): string;
  161. {---------------------------------------------------------------------
  162. Symbol helper routines
  163. ---------------------------------------------------------------------}
  164. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  165. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  166. Function SearchType(const hs:string;out size:tcgint): Boolean;
  167. Function SearchRecordType(const s:string): boolean;
  168. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  169. Function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  170. {---------------------------------------------------------------------
  171. Instruction generation routines
  172. ---------------------------------------------------------------------}
  173. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  174. Procedure ConcatConstant(p : TAsmList;value: tcgint; constsize:byte);
  175. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  176. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  177. Procedure ConcatString(p : TAsmList;s:string);
  178. procedure ConcatAlign(p:TAsmList;l:tcgint);
  179. Procedure ConcatPublic(p:TAsmList;const s : string);
  180. Procedure ConcatLocal(p:TAsmList;const s : string);
  181. Implementation
  182. uses
  183. SysUtils,
  184. defutil,systems,verbose,globals,
  185. symtable,paramgr,
  186. aasmcpu,
  187. procinfo;
  188. {*************************************************************************
  189. TExprParse
  190. *************************************************************************}
  191. Constructor TExprParse.create;
  192. Begin
  193. end;
  194. Procedure TExprParse.RPNPush(Num : tcgint);
  195. { Add an operand to the top of the RPN stack }
  196. begin
  197. if RPNTop < RPNMax then
  198. begin
  199. Inc(RPNTop);
  200. RPNStack[RPNTop]:=Num;
  201. end
  202. else
  203. Message(asmr_e_expr_illegal);
  204. end;
  205. Function TExprParse.RPNPop : tcgint;
  206. { Get the operand at the top of the RPN stack }
  207. begin
  208. RPNPop:=0;
  209. if RPNTop > 0 then
  210. begin
  211. RPNPop:=RPNStack[RPNTop];
  212. Dec(RPNTop);
  213. end
  214. else
  215. Message(asmr_e_expr_illegal);
  216. end;
  217. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  218. Var
  219. Temp : tcgint;
  220. n1,n2 : tcgint;
  221. LocalError : Integer;
  222. begin
  223. { Handle operators }
  224. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  225. Case Token[1] of
  226. '+' :
  227. Begin
  228. if not prefix then
  229. RPNPush(RPNPop + RPNPop);
  230. end;
  231. '-' :
  232. Begin
  233. if prefix then
  234. RPNPush(-(RPNPop))
  235. else
  236. begin
  237. n1:=RPNPop;
  238. n2:=RPNPop;
  239. RPNPush(n2 - n1);
  240. end;
  241. end;
  242. '*' : RPNPush(RPNPop * RPNPop);
  243. '&' :
  244. begin
  245. n1:=RPNPop;
  246. n2:=RPNPop;
  247. RPNPush(n2 and n1);
  248. end;
  249. '|' :
  250. begin
  251. n1:=RPNPop;
  252. n2:=RPNPop;
  253. RPNPush(n2 or n1);
  254. end;
  255. '~' : RPNPush(NOT RPNPop);
  256. '<' :
  257. begin
  258. n1:=RPNPop;
  259. n2:=RPNPop;
  260. RPNPush(n2 SHL n1);
  261. end;
  262. '>' :
  263. begin
  264. n1:=RPNPop;
  265. n2:=RPNPop;
  266. RPNPush(n2 SHR n1);
  267. end;
  268. '%' :
  269. begin
  270. Temp:=RPNPop;
  271. if Temp <> 0 then
  272. RPNPush(RPNPop mod Temp)
  273. else
  274. begin
  275. Message(asmr_e_expr_zero_divide);
  276. { push 1 for error recovery }
  277. RPNPush(1);
  278. end;
  279. end;
  280. '^' : RPNPush(RPNPop XOR RPNPop);
  281. '/' :
  282. begin
  283. Temp:=RPNPop;
  284. if Temp <> 0 then
  285. RPNPush(RPNPop div Temp)
  286. else
  287. begin
  288. Message(asmr_e_expr_zero_divide);
  289. { push 1 for error recovery }
  290. RPNPush(1);
  291. end;
  292. end;
  293. end
  294. else
  295. begin
  296. { Convert String to number and add to stack }
  297. Val(Token, Temp, LocalError);
  298. if LocalError = 0 then
  299. RPNPush(Temp)
  300. else
  301. begin
  302. Message(asmr_e_expr_illegal);
  303. { push 1 for error recovery }
  304. RPNPush(1);
  305. end;
  306. end;
  307. end;
  308. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  309. { Add an operator onto top of the stack }
  310. begin
  311. if OpTop < OpMax then
  312. begin
  313. Inc(OpTop);
  314. OpStack[OpTop].ch:=_Operator;
  315. OpStack[OpTop].is_prefix:=prefix;
  316. end
  317. else
  318. Message(asmr_e_expr_illegal);
  319. end;
  320. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  321. { Get operator at the top of the stack }
  322. begin
  323. if OpTop > 0 then
  324. begin
  325. _Operator:=OpStack[OpTop];
  326. Dec(OpTop);
  327. end
  328. else
  329. Message(asmr_e_expr_illegal);
  330. end;
  331. Function TExprParse.Priority(_Operator : Char) : aint;
  332. { Return priority of operator }
  333. { The greater the priority, the higher the precedence }
  334. begin
  335. Priority:=0;
  336. Case _Operator OF
  337. '(','[' :
  338. Priority:=0;
  339. '|','^','~' : // the lowest priority: OR, XOR, NOT
  340. Priority:=0;
  341. '&' : // bigger priority: AND
  342. Priority:=1;
  343. '+', '-' : // bigger priority: +, -
  344. Priority:=2;
  345. '*', '/','%','<','>' : // the highest priority: *, /, MOD, SHL, SHR
  346. Priority:=3;
  347. else
  348. Message(asmr_e_expr_illegal);
  349. end;
  350. end;
  351. Function TExprParse.Evaluate(Expr : String):tcgint;
  352. Var
  353. I : longint;
  354. Token : String;
  355. opr : TExprOperator;
  356. begin
  357. Evaluate:=0;
  358. { Reset stacks }
  359. OpTop :=0;
  360. RPNTop:=0;
  361. Token :='';
  362. { nothing to do ? }
  363. if Expr='' then
  364. exit;
  365. For I:=1 to Length(Expr) DO
  366. begin
  367. if Expr[I] in ['0'..'9'] then
  368. begin { Build multi-digit numbers }
  369. Token:=Token + Expr[I];
  370. if I = Length(Expr) then { Send last one to calculator }
  371. RPNCalc(Token,false);
  372. end
  373. else
  374. if Expr[I] in ['+', '-', '*', '/', '(', ')','[',']','^','&','|','%','~','<','>'] then
  375. begin
  376. if Token <> '' then
  377. begin { Send last built number to calc. }
  378. RPNCalc(Token,false);
  379. Token:='';
  380. end;
  381. Case Expr[I] OF
  382. '[' : OpPush('[',false);
  383. ']' : begin
  384. While (OpTop>0) and (OpStack[OpTop].ch <> '[') DO
  385. Begin
  386. OpPop(opr);
  387. RPNCalc(opr.ch,opr.is_prefix);
  388. end;
  389. OpPop(opr); { Pop off and ignore the '[' }
  390. end;
  391. '(' : OpPush('(',false);
  392. ')' : begin
  393. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  394. Begin
  395. OpPop(opr);
  396. RPNCalc(opr.ch,opr.is_prefix);
  397. end;
  398. OpPop(opr); { Pop off and ignore the '(' }
  399. end;
  400. '+','-','~' : Begin
  401. { workaround for -2147483648 }
  402. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  403. begin
  404. token:='-';
  405. expr[i]:='+';
  406. end;
  407. { if start of expression then surely a prefix }
  408. { or if previous char was also an operator }
  409. if (I = 1) or (not (Expr[I-1] in ['0'..'9',')'])) then
  410. OpPush(Expr[I],true)
  411. else
  412. Begin
  413. { Evaluate all higher priority operators }
  414. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  415. Begin
  416. OpPop(opr);
  417. RPNCalc(opr.ch,opr.is_prefix);
  418. end;
  419. OpPush(Expr[I],false);
  420. End;
  421. end;
  422. '*', '/',
  423. '^','|','&',
  424. '%','<','>' : begin
  425. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  426. Begin
  427. OpPop(opr);
  428. RPNCalc(opr.ch,opr.is_prefix);
  429. end;
  430. OpPush(Expr[I],false);
  431. end;
  432. end; { Case }
  433. end
  434. else
  435. Message(asmr_e_expr_illegal); { Handle bad input error }
  436. end;
  437. { Pop off the remaining operators }
  438. While OpTop > 0 do
  439. Begin
  440. OpPop(opr);
  441. RPNCalc(opr.ch,opr.is_prefix);
  442. end;
  443. { The result is stored on the top of the stack }
  444. Evaluate:=RPNPop;
  445. end;
  446. Destructor TExprParse.Destroy;
  447. Begin
  448. end;
  449. Function CalculateExpression(const expression: string): tcgint;
  450. var
  451. expr: TExprParse;
  452. Begin
  453. expr:=TExprParse.create;
  454. CalculateExpression:=expr.Evaluate(expression);
  455. expr.Free;
  456. end;
  457. {*************************************************************************}
  458. { String conversions/utils }
  459. {*************************************************************************}
  460. Function EscapeToPascal(const s:string): string;
  461. { converts a C styled string - which contains escape }
  462. { characters to a pascal style string. }
  463. var
  464. i,len : asizeint;
  465. hs : string;
  466. temp : string;
  467. c : char;
  468. Begin
  469. hs:='';
  470. len:=0;
  471. i:=0;
  472. while (i<length(s)) and (len<255) do
  473. begin
  474. Inc(i);
  475. if (s[i]='\') and (i<length(s)) then
  476. Begin
  477. inc(i);
  478. case s[i] of
  479. '\' :
  480. c:='\';
  481. 'b':
  482. c:=#8;
  483. 'f':
  484. c:=#12;
  485. 'n':
  486. c:=#10;
  487. 'r':
  488. c:=#13;
  489. 't':
  490. c:=#9;
  491. '"':
  492. c:='"';
  493. '0'..'7':
  494. Begin
  495. temp:=s[i];
  496. temp:=temp+s[i+1];
  497. temp:=temp+s[i+2];
  498. inc(i,2);
  499. c:=chr(ParseVal(temp,8));
  500. end;
  501. 'x':
  502. Begin
  503. temp:=s[i+1];
  504. temp:=temp+s[i+2];
  505. inc(i,2);
  506. c:=chr(ParseVal(temp,16));
  507. end;
  508. else
  509. Begin
  510. Message1(asmr_e_escape_seq_ignored,s[i]);
  511. c:=s[i];
  512. end;
  513. end;
  514. end
  515. else
  516. c:=s[i];
  517. inc(len);
  518. hs[len]:=c;
  519. end;
  520. hs[0]:=chr(len);
  521. EscapeToPascal:=hs;
  522. end;
  523. Function ParseVal(const S:String;base:byte):tcgint;
  524. { Converts a decimal string to tcgint }
  525. var
  526. code : integer;
  527. errmsg : word;
  528. prefix : string[2];
  529. Begin
  530. case base of
  531. 2 :
  532. begin
  533. errmsg:=asmr_e_error_converting_binary;
  534. prefix:='%';
  535. end;
  536. 8 :
  537. begin
  538. errmsg:=asmr_e_error_converting_octal;
  539. prefix:='&';
  540. end;
  541. 10 :
  542. begin
  543. errmsg:=asmr_e_error_converting_decimal;
  544. prefix:='';
  545. end;
  546. 16 :
  547. begin
  548. errmsg:=asmr_e_error_converting_hexadecimal;
  549. prefix:='$';
  550. end;
  551. else
  552. internalerror(200501202);
  553. end;
  554. val(prefix+s,result,code);
  555. if code<>0 then
  556. begin
  557. val(prefix+s,result,code);
  558. if code<>0 then
  559. begin
  560. Message1(errmsg,s);
  561. result:=0;
  562. end;
  563. end;
  564. end;
  565. Function PadZero(Var s: String; n: byte): Boolean;
  566. Begin
  567. PadZero:=TRUE;
  568. { Do some error checking first }
  569. if Length(s) = n then
  570. exit
  571. else
  572. if Length(s) > n then
  573. Begin
  574. PadZero:=FALSE;
  575. delete(s,n+1,length(s));
  576. exit;
  577. end
  578. else
  579. PadZero:=TRUE;
  580. { Fill it up with the specified character }
  581. fillchar(s[length(s)+1],n-1,#0);
  582. s[0]:=chr(n);
  583. end;
  584. {****************************************************************************
  585. TOperand
  586. ****************************************************************************}
  587. constructor TOperand.Create;
  588. begin
  589. size:=OS_NO;
  590. hasproc:=false;
  591. hastype:=false;
  592. hasvar:=false;
  593. FillChar(Opr,sizeof(Opr),0);
  594. end;
  595. destructor TOperand.destroy;
  596. begin
  597. end;
  598. Procedure TOperand.SetSize(_size:longint;force:boolean);
  599. begin
  600. if force or
  601. ((size = OS_NO) and (_size<=16)) then
  602. Begin
  603. case _size of
  604. 1 : size:=OS_8;
  605. 2 : size:=OS_16{ could be S_IS};
  606. 4 : size:=OS_32{ could be S_IL or S_FS};
  607. 8 : size:=OS_64{ could be S_D or S_FL};
  608. 10 : size:=OS_F80;
  609. 16 : size:=OS_128;
  610. end;
  611. end;
  612. end;
  613. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  614. begin
  615. end;
  616. function TOperand.SetupResult:boolean;
  617. begin
  618. SetupResult:=false;
  619. { replace by correct offset. }
  620. with current_procinfo.procdef do
  621. if (not is_void(returndef)) then
  622. begin
  623. if (m_tp7 in current_settings.modeswitches) and
  624. not (df_generic in defoptions) and
  625. (po_assembler in procoptions) and
  626. (not paramanager.ret_in_param(returndef,current_procinfo.procdef)) then
  627. begin
  628. message(asmr_e_cannot_use_RESULT_here);
  629. exit;
  630. end;
  631. SetupResult:=setupvar('result',false)
  632. end
  633. else
  634. message(asmr_e_void_function);
  635. end;
  636. Function TOperand.SetupSelf:boolean;
  637. Begin
  638. SetupSelf:=false;
  639. if assigned(current_structdef) then
  640. SetupSelf:=setupvar('self',false)
  641. else
  642. Message(asmr_e_cannot_use_SELF_outside_a_method);
  643. end;
  644. Function TOperand.SetupOldEBP:boolean;
  645. Begin
  646. SetupOldEBP:=false;
  647. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  648. SetupOldEBP:=setupvar('parentframe',false)
  649. else
  650. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  651. end;
  652. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  653. function symtable_has_localvarsyms(st:TSymtable):boolean;
  654. var
  655. sym : tsym;
  656. i : longint;
  657. begin
  658. result:=false;
  659. for i:=0 to st.SymList.Count-1 do
  660. begin
  661. sym:=tsym(st.SymList[i]);
  662. if sym.typ=localvarsym then
  663. begin
  664. result:=true;
  665. exit;
  666. end;
  667. end;
  668. end;
  669. procedure setconst(l:aint);
  670. begin
  671. { We return the address of the field, just like Delphi/TP }
  672. case opr.typ of
  673. OPR_NONE :
  674. begin
  675. opr.typ:=OPR_CONSTANT;
  676. opr.val:=l;
  677. end;
  678. OPR_CONSTANT :
  679. inc(opr.val,l);
  680. OPR_REFERENCE :
  681. inc(opr.ref.offset,l);
  682. OPR_LOCAL :
  683. inc(opr.localsymofs,l);
  684. else
  685. Message(asmr_e_invalid_operand_type);
  686. end;
  687. end;
  688. procedure setvarsize(sym: tabstractvarsym);
  689. var
  690. harrdef: tarraydef;
  691. l: asizeint;
  692. begin
  693. case sym.vardef.typ of
  694. orddef,
  695. enumdef,
  696. pointerdef,
  697. procvardef,
  698. floatdef :
  699. SetSize(sym.getsize,false);
  700. arraydef :
  701. begin
  702. { for arrays try to get the element size, take care of
  703. multiple indexes }
  704. harrdef:=tarraydef(sym.vardef);
  705. { calc array size }
  706. if is_special_array(harrdef) then
  707. l := -1
  708. else
  709. l := harrdef.size;
  710. case opr.typ of
  711. OPR_REFERENCE: opr.varsize := l;
  712. OPR_LOCAL: opr.localvarsize := l;
  713. end;
  714. while assigned(harrdef.elementdef) and
  715. (harrdef.elementdef.typ=arraydef) do
  716. harrdef:=tarraydef(harrdef.elementdef);
  717. if not is_packed_array(harrdef) then
  718. SetSize(harrdef.elesize,false)
  719. else
  720. if (harrdef.elepackedbitsize mod 8) = 0 then
  721. SetSize(harrdef.elepackedbitsize div 8,false);
  722. end;
  723. recorddef:
  724. case opr.typ of
  725. OPR_REFERENCE: opr.varsize := sym.getsize;
  726. OPR_LOCAL: opr.localvarsize := sym.getsize;
  727. end;
  728. end;
  729. end;
  730. { search and sets up the correct fields in the Instr record }
  731. { for the NON-constant identifier passed to the routine. }
  732. { if not found returns FALSE. }
  733. var
  734. sym : tsym;
  735. srsymtable : TSymtable;
  736. {$ifdef x86}
  737. segreg,
  738. {$endif x86}
  739. indexreg : tregister;
  740. plist : ppropaccesslistitem;
  741. size_set_from_absolute : boolean = false;
  742. { offset fixup (in bytes), coming from an absolute declaration with an index
  743. (e.g. var tralala: word absolute moo[5]; ) }
  744. absoffset: asizeint=0;
  745. harrdef: tarraydef;
  746. tmpprocinfo: tprocinfo;
  747. Begin
  748. SetupVar:=false;
  749. asmsearchsym(s,sym,srsymtable);
  750. if sym = nil then
  751. exit;
  752. if sym.typ=absolutevarsym then
  753. begin
  754. case tabsolutevarsym(sym).abstyp of
  755. tovar:
  756. begin
  757. { Only support simple loads }
  758. plist:=tabsolutevarsym(sym).ref.firstsym;
  759. if assigned(plist) and
  760. (plist^.sltype=sl_load) then
  761. begin
  762. setvarsize(tabstractvarsym(sym));
  763. size_set_from_absolute:=true;
  764. sym:=plist^.sym;
  765. { resolve the chain of array indexes (if there are any) }
  766. harrdef:=nil;
  767. while assigned(plist^.next) do
  768. begin
  769. plist:=plist^.next;
  770. if (plist^.sltype=sl_vec) and (tabstractvarsym(sym).vardef.typ=arraydef) then
  771. begin
  772. if harrdef=nil then
  773. harrdef:=tarraydef(tabstractvarsym(sym).vardef)
  774. else if harrdef.elementdef.typ=arraydef then
  775. harrdef:=tarraydef(harrdef.elementdef)
  776. else
  777. begin
  778. Message(asmr_e_unsupported_symbol_type);
  779. exit;
  780. end;
  781. if is_special_array(harrdef) then
  782. begin
  783. Message(asmr_e_unsupported_symbol_type);
  784. exit;
  785. end;
  786. if not is_packed_array(harrdef) then
  787. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange))*harrdef.elesize)
  788. else if (Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize mod 8)=0 then
  789. Inc(absoffset,asizeint(Int64(plist^.value-harrdef.lowrange)*harrdef.elepackedbitsize div 8))
  790. else
  791. Message(asmr_e_packed_element);
  792. end
  793. else
  794. begin
  795. Message(asmr_e_unsupported_symbol_type);
  796. exit;
  797. end;
  798. end;
  799. end
  800. else
  801. begin
  802. Message(asmr_e_unsupported_symbol_type);
  803. exit;
  804. end;
  805. end;
  806. toaddr:
  807. begin
  808. initref;
  809. opr.ref.offset:=tabsolutevarsym(sym).addroffset;
  810. setvarsize(tabstractvarsym(sym));
  811. size_set_from_absolute:=true;
  812. hasvar:=true;
  813. Result:=true;
  814. exit;
  815. end;
  816. else
  817. begin
  818. Message(asmr_e_unsupported_symbol_type);
  819. exit;
  820. end;
  821. end;
  822. end;
  823. case sym.typ of
  824. fieldvarsym :
  825. begin
  826. if not tabstractrecordsymtable(sym.owner).is_packed then
  827. setconst(absoffset+tfieldvarsym(sym).fieldoffset)
  828. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  829. setconst(absoffset+tfieldvarsym(sym).fieldoffset div 8)
  830. else
  831. Message(asmr_e_packed_element);
  832. if not size_set_from_absolute then
  833. setvarsize(tabstractvarsym(sym));
  834. hasvar:=true;
  835. SetupVar:=true;
  836. end;
  837. staticvarsym,
  838. localvarsym,
  839. paravarsym :
  840. begin
  841. { we always assume in asm statements that }
  842. { that the variable is valid. }
  843. tabstractvarsym(sym).varstate:=vs_readwritten;
  844. inc(tabstractvarsym(sym).refs);
  845. { variable can't be placed in a register }
  846. tabstractvarsym(sym).varregable:=vr_none;
  847. { and anything may happen with its address }
  848. tabstractvarsym(sym).addr_taken:=true;
  849. case sym.typ of
  850. staticvarsym :
  851. begin
  852. initref;
  853. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tstaticvarsym(sym).mangledname,AT_DATA);
  854. Inc(opr.ref.offset,absoffset);
  855. end;
  856. paravarsym,
  857. localvarsym :
  858. begin
  859. tmpprocinfo:=current_procinfo;
  860. while assigned(tmpprocinfo) do
  861. begin
  862. if (sym.owner=tmpprocinfo.procdef.localst) or
  863. (sym.owner=tmpprocinfo.procdef.parast) then
  864. begin
  865. tmpprocinfo.procdef.init_paraloc_info(calleeside);
  866. break;
  867. end;
  868. tmpprocinfo:=tmpprocinfo.parent;
  869. end;
  870. if opr.typ=OPR_REFERENCE then
  871. begin
  872. {$ifdef x86}
  873. segreg:=opr.ref.segment;
  874. {$endif x86}
  875. indexreg:=opr.ref.base;
  876. if opr.ref.index<>NR_NO then
  877. begin
  878. if indexreg=NR_NO then
  879. indexreg:=opr.ref.index
  880. else
  881. Message(asmr_e_multiple_index);
  882. end;
  883. end
  884. else
  885. begin
  886. {$ifdef x86}
  887. segreg:=NR_NO;
  888. {$endif x86}
  889. indexreg:=NR_NO;
  890. end;
  891. opr.typ:=OPR_LOCAL;
  892. if assigned(current_procinfo.parent) and
  893. not(po_inline in current_procinfo.procdef.procoptions) and
  894. (sym.owner<>current_procinfo.procdef.localst) and
  895. (sym.owner<>current_procinfo.procdef.parast) and
  896. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  897. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  898. message1(asmr_e_local_para_unreachable,s);
  899. opr.localsym:=tabstractnormalvarsym(sym);
  900. opr.localsymofs:=absoffset;
  901. {$ifdef x86}
  902. opr.localsegment:=segreg;
  903. {$endif x86}
  904. opr.localindexreg:=indexreg;
  905. opr.localscale:=0;
  906. opr.localgetoffset:=GetOffset;
  907. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vardef,current_procinfo.procdef.proccalloption) then
  908. SetSize(sizeof(pint),false);
  909. end;
  910. end;
  911. if not size_set_from_absolute then
  912. setvarsize(tabstractvarsym(sym));
  913. hasvar:=true;
  914. SetupVar:=true;
  915. Exit;
  916. end;
  917. constsym :
  918. begin
  919. if tconstsym(sym).consttyp=constord then
  920. begin
  921. setconst(tconstsym(sym).value.valueord.svalue);
  922. SetupVar:=true;
  923. Exit;
  924. end;
  925. end;
  926. typesym :
  927. begin
  928. if ttypesym(sym).typedef.typ in [recorddef,objectdef] then
  929. begin
  930. setconst(0);
  931. SetupVar:=TRUE;
  932. Exit;
  933. end;
  934. end;
  935. procsym :
  936. begin
  937. if Tprocsym(sym).ProcdefList.Count>1 then
  938. Message(asmr_w_calling_overload_func);
  939. case opr.typ of
  940. OPR_REFERENCE:
  941. begin
  942. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  943. Inc(opr.ref.offset,absoffset);
  944. {$ifdef i8086}
  945. opr.ref_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  946. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  947. {$endif i8086}
  948. end;
  949. OPR_NONE:
  950. begin
  951. opr.typ:=OPR_SYMBOL;
  952. opr.symbol:=current_asmdata.RefAsmSymbol(tprocdef(tprocsym(sym).ProcdefList[0]).mangledname,AT_FUNCTION);
  953. {$ifdef i8086}
  954. opr.sym_farproc_entry:=is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  955. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions);
  956. {$endif i8086}
  957. opr.symofs:=absoffset;
  958. end;
  959. else
  960. Message(asmr_e_invalid_operand_type);
  961. end;
  962. hasproc:=true;
  963. hasvar:=true;
  964. SetupVar:=TRUE;
  965. Exit;
  966. end;
  967. {$ifdef i8086}
  968. labelsym :
  969. begin
  970. case opr.typ of
  971. OPR_REFERENCE:
  972. begin
  973. opr.ref.symbol:=current_asmdata.RefAsmSymbol(tlabelsym(sym).mangledname,AT_FUNCTION);
  974. Inc(opr.ref.offset,absoffset);
  975. if opr.ref.segment=NR_NO then
  976. opr.ref.segment:=NR_CS;
  977. end;
  978. else
  979. begin
  980. Message(asmr_e_unsupported_symbol_type);
  981. exit;
  982. end;
  983. end;
  984. haslabelref:=true;
  985. hasvar:=true;
  986. SetupVar:=TRUE;
  987. Exit;
  988. end
  989. {$endif i8086}
  990. else
  991. begin
  992. Message(asmr_e_unsupported_symbol_type);
  993. exit;
  994. end;
  995. end;
  996. end;
  997. procedure TOperand.InitRef;
  998. {*********************************************************************}
  999. { Description: This routine first check if the opcode is of }
  1000. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  1001. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  1002. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  1003. { to point to the default segment. }
  1004. {*********************************************************************}
  1005. var
  1006. l : aint;
  1007. hsymofs : aint;
  1008. hsymbol : tasmsymbol;
  1009. reg : tregister;
  1010. hsym_farprocentry: Boolean;
  1011. Begin
  1012. case opr.typ of
  1013. OPR_REFERENCE :
  1014. exit;
  1015. OPR_CONSTANT :
  1016. begin
  1017. l:=opr.val;
  1018. opr.typ:=OPR_REFERENCE;
  1019. Fillchar(opr.ref,sizeof(treference),0);
  1020. opr.Ref.Offset:=l;
  1021. opr.varsize:=0;
  1022. opr.constoffset:=0;
  1023. opr.ref_farproc_entry:=false;
  1024. end;
  1025. OPR_NONE :
  1026. begin
  1027. opr.typ:=OPR_REFERENCE;
  1028. opr.varsize:=0;
  1029. opr.constoffset:=0;
  1030. opr.ref_farproc_entry:=false;
  1031. Fillchar(opr.ref,sizeof(treference),0);
  1032. end;
  1033. OPR_REGISTER :
  1034. begin
  1035. reg:=opr.reg;
  1036. opr.typ:=OPR_REFERENCE;
  1037. opr.varsize:=0;
  1038. opr.constoffset:=0;
  1039. opr.ref_farproc_entry:=false;
  1040. Fillchar(opr.ref,sizeof(treference),0);
  1041. opr.Ref.base:=reg;
  1042. end;
  1043. OPR_SYMBOL :
  1044. begin
  1045. hsymbol:=opr.symbol;
  1046. hsymofs:=opr.symofs;
  1047. hsym_farprocentry:=opr.sym_farproc_entry;
  1048. opr.typ:=OPR_REFERENCE;
  1049. opr.varsize:=0;
  1050. opr.constoffset:=0;
  1051. Fillchar(opr.ref,sizeof(treference),0);
  1052. opr.ref.symbol:=hsymbol;
  1053. opr.ref.offset:=hsymofs;
  1054. opr.ref_farproc_entry:=hsym_farprocentry;
  1055. end;
  1056. else
  1057. InitRefError;
  1058. end;
  1059. end;
  1060. procedure TOperand.InitRefConvertLocal;
  1061. var
  1062. localvarsize,localconstoffset: asizeint;
  1063. localsym:tabstractnormalvarsym;
  1064. localsymofs:aint;
  1065. {$ifdef x86}
  1066. localsegment,
  1067. {$endif x86}
  1068. localindexreg:tregister;
  1069. localscale:byte;
  1070. begin
  1071. if opr.typ=OPR_LOCAL then
  1072. begin
  1073. if AsmRegisterPara(opr.localsym) and
  1074. not opr.localgetoffset then
  1075. begin
  1076. localvarsize:=opr.localvarsize;
  1077. localconstoffset:=opr.localconstoffset;
  1078. localsym:=opr.localsym;
  1079. localsymofs:=opr.localsymofs;
  1080. {$ifdef x86}
  1081. localsegment:=opr.localsegment;
  1082. {$endif x86}
  1083. localindexreg:=opr.localindexreg;
  1084. localscale:=opr.localscale;;
  1085. opr.typ:=OPR_REFERENCE;
  1086. hasvar:=false;
  1087. Fillchar(opr.ref,sizeof(treference),0);
  1088. opr.varsize:=localvarsize;
  1089. opr.constoffset:=localconstoffset;
  1090. opr.ref_farproc_entry:=false;
  1091. opr.ref.base:=tparavarsym(localsym).paraloc[calleeside].Location^.register;
  1092. opr.ref.offset:=localsymofs;
  1093. {$ifdef x86}
  1094. opr.ref.segment:=localsegment;
  1095. {$endif x86}
  1096. opr.ref.index:=localindexreg;
  1097. opr.ref.scalefactor:=localscale;
  1098. end
  1099. else
  1100. InitRefError;
  1101. end
  1102. else
  1103. InitRef;
  1104. end;
  1105. procedure TOperand.InitRefError;
  1106. begin
  1107. Message(asmr_e_invalid_operand_type);
  1108. { Recover }
  1109. opr.typ:=OPR_REFERENCE;
  1110. opr.varsize:=0;
  1111. opr.constoffset:=0;
  1112. opr.ref_farproc_entry:=false;
  1113. Fillchar(opr.ref,sizeof(treference),0);
  1114. end;
  1115. Function TOperand.CheckOperand: boolean;
  1116. {*********************************************************************}
  1117. { Description: This routine checks if the operand is of }
  1118. { valid, and returns false if it isn't. Does nothing by default. }
  1119. {*********************************************************************}
  1120. begin
  1121. result:=true;
  1122. end;
  1123. {****************************************************************************
  1124. TInstruction
  1125. ****************************************************************************}
  1126. constructor TInstruction.create(optype : tcoperand);
  1127. var
  1128. i : longint;
  1129. Begin
  1130. { these field are set to 0 anyways by the constructor helper (FK)
  1131. Opcode:=A_NONE;
  1132. Condition:=C_NONE;
  1133. Ops:=0;
  1134. }
  1135. filepos:=current_filepos;
  1136. for i:=1 to max_operands do
  1137. Operands[i]:=optype.create;
  1138. Labeled:=false;
  1139. end;
  1140. destructor TInstruction.destroy;
  1141. var
  1142. i : longint;
  1143. Begin
  1144. for i:=1 to max_operands do
  1145. Operands[i].free;
  1146. end;
  1147. function TInstruction.ConcatInstruction(p:TAsmList) : tai;
  1148. var
  1149. ai : taicpu;
  1150. i : longint;
  1151. begin
  1152. for i:=1 to Ops do
  1153. operands[i].CheckOperand;
  1154. ai:=taicpu.op_none(opcode);
  1155. ai.fileinfo:=filepos;
  1156. ai.Ops:=Ops;
  1157. ai.Allocate_oper(Ops);
  1158. for i:=1 to Ops do
  1159. with operands[i].opr do
  1160. begin
  1161. case typ of
  1162. OPR_CONSTANT :
  1163. ai.loadconst(i-1,val);
  1164. OPR_REGISTER:
  1165. ai.loadreg(i-1,reg);
  1166. OPR_SYMBOL:
  1167. ai.loadsymbol(i-1,symbol,symofs);
  1168. OPR_LOCAL :
  1169. begin
  1170. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1171. localscale,localgetoffset,localforceref);
  1172. {$ifdef x86}
  1173. ai.oper[i-1]^.localoper^.localsegment:=localsegment;
  1174. {$endif x86}
  1175. end;
  1176. OPR_REFERENCE:
  1177. ai.loadref(i-1,ref);
  1178. {$ifdef m68k}
  1179. OPR_REGSET:
  1180. ai.loadregset(i-1,regsetdata,regsetaddr,regsetfpu);
  1181. OPR_REGPAIR:
  1182. ai.loadregpair(i-1,reghi,reglo);
  1183. {$endif}
  1184. {$ifdef ARM}
  1185. OPR_REGSET:
  1186. ai.loadregset(i-1,regtype,subreg,regset,usermode);
  1187. OPR_MODEFLAGS:
  1188. ai.loadmodeflags(i-1,flags);
  1189. OPR_SPECIALREG:
  1190. ai.loadspecialreg(i-1,specialreg,specialregflags);
  1191. {$endif ARM}
  1192. {$if defined(arm) or defined(aarch64)}
  1193. OPR_SHIFTEROP:
  1194. ai.loadshifterop(i-1,shifterop);
  1195. OPR_COND:
  1196. ai.loadconditioncode(i-1,cc);
  1197. {$endif arm or aarch64}
  1198. { ignore wrong operand }
  1199. OPR_NONE:
  1200. ;
  1201. else
  1202. internalerror(200501051);
  1203. end;
  1204. end;
  1205. ai.SetCondition(condition);
  1206. { Concat the opcode or give an error }
  1207. if assigned(ai) then
  1208. p.concat(ai)
  1209. else
  1210. Message(asmr_e_invalid_opcode_and_operand);
  1211. result:=ai;
  1212. end;
  1213. {****************************************************************************
  1214. Symbol table helper routines
  1215. ****************************************************************************}
  1216. procedure AsmSearchSym(const s:string;out srsym:tsym;out srsymtable:TSymtable);
  1217. var
  1218. i : integer;
  1219. begin
  1220. i:=pos('.',s);
  1221. { allow unit.identifier }
  1222. if i>0 then
  1223. begin
  1224. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1225. if assigned(srsym) then
  1226. begin
  1227. if (srsym.typ=unitsym) and
  1228. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1229. srsym.owner.iscurrentunit then
  1230. searchsym_in_module(tunitsym(srsym).module,Copy(s,i+1,255),srsym,srsymtable)
  1231. else
  1232. begin
  1233. srsym:=nil;
  1234. srsymtable:=nil;
  1235. end;
  1236. end;
  1237. end
  1238. else
  1239. searchsym(s,srsym,srsymtable);
  1240. { in asm routines, the function result variable, that matches the function
  1241. name should be avoided, because:
  1242. 1) there's already a @Result directive (even in TP7) that can be used, if
  1243. you want to access the function result
  1244. 2) there's no other way to disambiguate between the function result variable
  1245. and the function's address (using asm syntax only)
  1246. This fixes code, such as:
  1247. function test1: word;
  1248. begin
  1249. asm
  1250. mov ax, offset test1
  1251. end;
  1252. end;
  1253. and makes it work in a consistent manner as this code:
  1254. procedure test2;
  1255. begin
  1256. asm
  1257. mov ax, offset test2
  1258. end;
  1259. end; }
  1260. if assigned(srsym) and
  1261. assigned(srsymtable) and
  1262. (srsym.typ=absolutevarsym) and
  1263. (vo_is_funcret in tabsolutevarsym(srsym).varoptions) and
  1264. (srsymtable.symtabletype=localsymtable) and
  1265. assigned(srsymtable.defowner) and
  1266. (srsymtable.defowner.typ=procdef) and
  1267. (tprocdef(srsymtable.defowner).procsym.name=tabsolutevarsym(srsym).Name) then
  1268. begin
  1269. srsym:=tprocdef(srsymtable.defowner).procsym;
  1270. srsymtable:=srsym.Owner;
  1271. end;
  1272. end;
  1273. Function SearchType(const hs:string;out size:tcgint): Boolean;
  1274. var
  1275. srsym : tsym;
  1276. srsymtable : TSymtable;
  1277. begin
  1278. result:=false;
  1279. size:=0;
  1280. asmsearchsym(hs,srsym,srsymtable);
  1281. if assigned(srsym) and
  1282. (srsym.typ=typesym) then
  1283. begin
  1284. size:=ttypesym(srsym).typedef.size;
  1285. result:=true;
  1286. end;
  1287. end;
  1288. Function SearchRecordType(const s:string): boolean;
  1289. var
  1290. srsym : tsym;
  1291. srsymtable : TSymtable;
  1292. Begin
  1293. SearchRecordType:=false;
  1294. { Check the constants in symtable }
  1295. asmsearchsym(s,srsym,srsymtable);
  1296. if srsym <> nil then
  1297. Begin
  1298. case srsym.typ of
  1299. typesym :
  1300. begin
  1301. if ttypesym(srsym).typedef.typ in [recorddef,objectdef] then
  1302. begin
  1303. SearchRecordType:=true;
  1304. exit;
  1305. end;
  1306. end;
  1307. fieldvarsym :
  1308. begin
  1309. if (tfieldvarsym(srsym).vardef.typ in [recorddef,objectdef]) then
  1310. begin
  1311. SearchRecordType:=true;
  1312. exit;
  1313. end;
  1314. end;
  1315. end;
  1316. end;
  1317. end;
  1318. Function SearchIConstant(const s:string; var l:tcgint): boolean;
  1319. {**********************************************************************}
  1320. { Description: Searches for a CONSTANT of name s in either the local }
  1321. { symbol list, then in the global symbol list, and returns the value }
  1322. { of that constant in l. Returns TRUE if successfull, if not found, }
  1323. { or if the constant is not of correct type, then returns FALSE }
  1324. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1325. { respectively. }
  1326. {**********************************************************************}
  1327. var
  1328. srsym : tsym;
  1329. srsymtable : TSymtable;
  1330. Begin
  1331. SearchIConstant:=false;
  1332. { check for TRUE or FALSE reserved words first }
  1333. if s = 'TRUE' then
  1334. Begin
  1335. SearchIConstant:=TRUE;
  1336. l:=1;
  1337. exit;
  1338. end;
  1339. if s = 'FALSE' then
  1340. Begin
  1341. SearchIConstant:=TRUE;
  1342. l:=0;
  1343. exit;
  1344. end;
  1345. { Check the constants in symtable }
  1346. asmsearchsym(s,srsym,srsymtable);
  1347. if srsym <> nil then
  1348. Begin
  1349. case srsym.typ of
  1350. constsym :
  1351. begin
  1352. if tconstsym(srsym).consttyp=constord then
  1353. Begin
  1354. l:=tconstsym(srsym).value.valueord.svalue;
  1355. SearchIConstant:=TRUE;
  1356. exit;
  1357. end;
  1358. end;
  1359. enumsym:
  1360. Begin
  1361. l:=tenumsym(srsym).value;
  1362. SearchIConstant:=TRUE;
  1363. exit;
  1364. end;
  1365. end;
  1366. end;
  1367. end;
  1368. function AsmRegisterPara(sym: tabstractnormalvarsym): boolean;
  1369. begin
  1370. result:=
  1371. (po_assembler in current_procinfo.procdef.procoptions) and
  1372. (sym.typ=paravarsym) and
  1373. (tparavarsym(sym).paraloc[calleeside].Location^.Loc=LOC_REGISTER);
  1374. end;
  1375. Function GetRecordOffsetSize(s:string;out Offset: tcgint;out Size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean):boolean;
  1376. { search and returns the offset and size of records/objects of the base }
  1377. { with field name setup in field. }
  1378. { returns FALSE if not found. }
  1379. { used when base is a variable or a typed constant name. }
  1380. var
  1381. st : TSymtable;
  1382. harrdef : tarraydef;
  1383. sym : tsym;
  1384. srsymtable : TSymtable;
  1385. i : longint;
  1386. base : string;
  1387. procdef: tprocdef;
  1388. Begin
  1389. GetRecordOffsetSize:=FALSE;
  1390. Offset:=0;
  1391. Size:=0;
  1392. mangledname:='';
  1393. hastypecast:=false;
  1394. i:=pos('.',s);
  1395. if i=0 then
  1396. i:=255;
  1397. base:=Copy(s,1,i-1);
  1398. delete(s,1,i);
  1399. if base='SELF' then
  1400. st:=current_structdef.symtable
  1401. else
  1402. begin
  1403. asmsearchsym(base,sym,srsymtable);
  1404. { allow unitname.identifier }
  1405. if assigned(sym) and (sym.typ=unitsym) then
  1406. begin
  1407. i:=pos('.',s);
  1408. if i=0 then
  1409. i:=255;
  1410. base:=base+'.'+Copy(s,1,i-1);
  1411. delete(s,1,i);
  1412. asmsearchsym(base,sym,srsymtable);
  1413. end;
  1414. st:=nil;
  1415. { we can start with a var,type,typedconst }
  1416. if assigned(sym) then
  1417. case sym.typ of
  1418. staticvarsym,
  1419. localvarsym,
  1420. paravarsym :
  1421. st:=Tabstractvarsym(sym).vardef.GetSymtable(gs_record);
  1422. typesym :
  1423. st:=Ttypesym(sym).typedef.GetSymtable(gs_record);
  1424. end
  1425. else
  1426. s:='';
  1427. end;
  1428. { now walk all recordsymtables }
  1429. while assigned(st) and (s<>'') do
  1430. begin
  1431. { load next field in base }
  1432. i:=pos('.',s);
  1433. if i=0 then
  1434. i:=255;
  1435. base:=Copy(s,1,i-1);
  1436. delete(s,1,i);
  1437. sym:=search_struct_member(tabstractrecorddef(st.defowner),base);
  1438. if not assigned(sym) then
  1439. begin
  1440. GetRecordOffsetSize:=false;
  1441. exit;
  1442. end;
  1443. st:=nil;
  1444. case sym.typ of
  1445. fieldvarsym :
  1446. with Tfieldvarsym(sym) do
  1447. begin
  1448. if not tabstractrecordsymtable(sym.owner).is_packed then
  1449. inc(Offset,fieldoffset)
  1450. else if tfieldvarsym(sym).fieldoffset mod 8 = 0 then
  1451. inc(Offset,fieldoffset div 8)
  1452. else
  1453. Message(asmr_e_packed_element);
  1454. size:=getsize;
  1455. case vardef.typ of
  1456. arraydef :
  1457. begin
  1458. { for arrays try to get the element size, take care of
  1459. multiple indexes }
  1460. harrdef:=tarraydef(vardef);
  1461. while assigned(harrdef.elementdef) and
  1462. (harrdef.elementdef.typ=arraydef) do
  1463. harrdef:=tarraydef(harrdef.elementdef);
  1464. if not is_packed_array(harrdef) then
  1465. size:=harrdef.elesize
  1466. else
  1467. begin
  1468. if (harrdef.elepackedbitsize mod 8) <> 0 then
  1469. Message(asmr_e_packed_element);
  1470. size := (harrdef.elepackedbitsize + 7) div 8;
  1471. end;
  1472. end;
  1473. recorddef :
  1474. st:=trecorddef(vardef).symtable;
  1475. objectdef :
  1476. st:=tobjectdef(vardef).symtable;
  1477. end;
  1478. end;
  1479. procsym:
  1480. begin
  1481. st:=nil;
  1482. if Tprocsym(sym).ProcdefList.Count>1 then
  1483. Message(asmr_w_calling_overload_func);
  1484. procdef:=tprocdef(tprocsym(sym).ProcdefList[0]);
  1485. if (not needvmtofs) then
  1486. begin
  1487. mangledname:=procdef.mangledname;
  1488. end
  1489. else
  1490. begin
  1491. { can only get the vmtoffset of virtual methods }
  1492. if not(po_virtualmethod in procdef.procoptions) or
  1493. is_objectpascal_helper(procdef.struct) then
  1494. Message1(asmr_e_no_vmtoffset_possible,FullTypeName(procdef,nil))
  1495. else
  1496. begin
  1497. { size = sizeof(target_system_pointer) }
  1498. size:=sizeof(pint);
  1499. offset:=tobjectdef(procdef.struct).vmtmethodoffset(procdef.extnumber)
  1500. end;
  1501. end;
  1502. { if something comes after the procsym, it's invalid assembler syntax }
  1503. GetRecordOffsetSize:=(s='');
  1504. exit;
  1505. end;
  1506. end;
  1507. end;
  1508. { Support Field.Type as typecasting }
  1509. if (st=nil) and (s<>'') then
  1510. begin
  1511. asmsearchsym(s,sym,srsymtable);
  1512. if assigned(sym) and (sym.typ=typesym) then
  1513. begin
  1514. size:=ttypesym(sym).typedef.size;
  1515. s:='';
  1516. hastypecast:=true;
  1517. end;
  1518. end;
  1519. GetRecordOffsetSize:=(s='');
  1520. end;
  1521. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1522. var
  1523. sym : tsym;
  1524. srsymtable : TSymtable;
  1525. hs : string;
  1526. Begin
  1527. hl:=nil;
  1528. SearchLabel:=false;
  1529. { Check for pascal labels, which are case insensetive }
  1530. hs:=upper(s);
  1531. asmsearchsym(hs,sym,srsymtable);
  1532. if sym=nil then
  1533. exit;
  1534. case sym.typ of
  1535. labelsym :
  1536. begin
  1537. if symtablestack.top.symtablelevel<>srsymtable.symtablelevel then
  1538. begin
  1539. if (srsymtable.symtabletype=globalsymtable) or create_smartlink_library then
  1540. Tlabelsym(sym).nonlocal:=true;
  1541. if emit then
  1542. exclude(current_procinfo.procdef.procoptions,po_inline);
  1543. end;
  1544. if not(assigned(tlabelsym(sym).asmblocklabel)) then
  1545. if Tlabelsym(sym).nonlocal then
  1546. current_asmdata.getglobaljumplabel(tlabelsym(sym).asmblocklabel)
  1547. else
  1548. current_asmdata.getjumplabel(tlabelsym(sym).asmblocklabel);
  1549. hl:=tlabelsym(sym).asmblocklabel;
  1550. if emit then
  1551. begin
  1552. if tlabelsym(sym).defined then
  1553. Message(sym_e_label_already_defined);
  1554. tlabelsym(sym).defined:=true
  1555. end
  1556. else
  1557. tlabelsym(sym).used:=true;
  1558. SearchLabel:=true;
  1559. end;
  1560. end;
  1561. end;
  1562. {*************************************************************************}
  1563. { Instruction Generation Utilities }
  1564. {*************************************************************************}
  1565. Procedure ConcatString(p : TAsmList;s:string);
  1566. {*********************************************************************}
  1567. { PROCEDURE ConcatString(s:string); }
  1568. { Description: This routine adds the character chain pointed to in }
  1569. { s to the instruction linked list. }
  1570. {*********************************************************************}
  1571. Begin
  1572. p.concat(Tai_string.Create(s));
  1573. end;
  1574. Procedure ConcatConstant(p: TAsmList; value: tcgint; constsize:byte);
  1575. {*********************************************************************}
  1576. { Description: This routine adds the value constant to the current }
  1577. { instruction linked list. }
  1578. {*********************************************************************}
  1579. var
  1580. rangelo,rangehi : int64;
  1581. Begin
  1582. case constsize of
  1583. 1 :
  1584. begin
  1585. p.concat(Tai_const.Create_8bit(byte(value)));
  1586. rangelo:=low(shortint);
  1587. rangehi:=high(byte);
  1588. end;
  1589. 2 :
  1590. begin
  1591. p.concat(Tai_const.Create_16bit(word(value)));
  1592. rangelo:=low(smallint);
  1593. rangehi:=high(word);
  1594. end;
  1595. 4 :
  1596. begin
  1597. p.concat(Tai_const.Create_32bit(longint(value)));
  1598. rangelo:=low(longint);
  1599. rangehi:=high(cardinal);
  1600. end;
  1601. 8 :
  1602. begin
  1603. p.concat(Tai_const.Create_64bit(int64(value)));
  1604. rangelo:=0;
  1605. rangehi:=0;
  1606. end;
  1607. else
  1608. internalerror(200405011);
  1609. end;
  1610. { check for out of bounds }
  1611. if (rangelo<>0) and
  1612. ((value>rangehi) or (value<rangelo)) then
  1613. Message(asmr_e_constant_out_of_bounds);
  1614. end;
  1615. Procedure ConcatConstSymbol(p : TAsmList;const sym:string;symtyp:tasmsymtype;l:tcgint;constsize:byte;isofs:boolean);
  1616. begin
  1617. {$ifdef i8086}
  1618. { 'DW xx' as well as 'DW OFFSET xx' are just near pointers }
  1619. if constsize=2 then
  1620. p.concat(Tai_const.Createname_near(sym,l))
  1621. else if constsize=4 then
  1622. begin
  1623. if isofs then
  1624. begin
  1625. { 'DD OFFSET xx' is a 32-bit offset; since we don't produce 32-bit
  1626. relocations yet, just do a 16-bit one and set the high word to 0 }
  1627. p.concat(Tai_const.Createname_near(sym,l));
  1628. p.concat(Tai_const.Create_16bit(0));
  1629. end
  1630. else
  1631. { 'DD xx' is a far pointer }
  1632. p.concat(Tai_const.Createname_far(sym,l));
  1633. end
  1634. else
  1635. internalerror(2018020701);
  1636. {$else i8086}
  1637. p.concat(Tai_const.Createname(sym,l));
  1638. {$endif i8086}
  1639. end;
  1640. Procedure ConcatRealConstant(p : TAsmList;value: bestreal; real_typ : tfloattype);
  1641. {***********************************************************************}
  1642. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1643. { Description: This routine adds the value constant to the current }
  1644. { instruction linked list. }
  1645. { real_typ -> indicates the type of the real data to initialize: }
  1646. { s32real -> create a single node. }
  1647. { s64real -> create a double node. }
  1648. { s80real -> create an extended node. }
  1649. { s64bit -> create a comp node. }
  1650. { f32bit -> create a fixed node. (not used normally) }
  1651. {***********************************************************************}
  1652. Begin
  1653. case real_typ of
  1654. s32real : p.concat(tai_realconst.create_s32real(value));
  1655. s64real :
  1656. {$ifdef ARM}
  1657. if is_double_hilo_swapped then
  1658. p.concat(tai_realconst.create_s64real_hiloswapped(value))
  1659. else
  1660. {$endif ARM}
  1661. p.concat(tai_realconst.create_s64real(value));
  1662. s80real : p.concat(tai_realconst.create_s80real(value,s80floattype.size));
  1663. sc80real : p.concat(tai_realconst.create_s80real(value,sc80floattype.size));
  1664. s64comp : p.concat(tai_realconst.create_s64compreal(trunc(value)));
  1665. else
  1666. internalerror(2014050608);
  1667. end;
  1668. end;
  1669. Procedure ConcatLabel(p: TAsmList;var l : tasmlabel);
  1670. {*********************************************************************}
  1671. { PROCEDURE ConcatLabel }
  1672. { Description: This routine either emits a label or a labeled }
  1673. { instruction to the linked list of instructions. }
  1674. {*********************************************************************}
  1675. begin
  1676. p.concat(Tai_label.Create(l));
  1677. end;
  1678. procedure ConcatAlign(p:TAsmList;l:tcgint);
  1679. {*********************************************************************}
  1680. { PROCEDURE ConcatPublic }
  1681. { Description: This routine emits an global definition to the }
  1682. { linked list of instructions.(used by AT&T styled asm) }
  1683. {*********************************************************************}
  1684. begin
  1685. p.concat(Tai_align.Create(l));
  1686. end;
  1687. procedure ConcatPublic(p:TAsmList;const s : string);
  1688. {*********************************************************************}
  1689. { PROCEDURE ConcatPublic }
  1690. { Description: This routine emits an global definition to the }
  1691. { linked list of instructions.(used by AT&T styled asm) }
  1692. {*********************************************************************}
  1693. begin
  1694. p.concat(Tai_symbol.Createname_global(s,AT_LABEL,0,voidcodepointertype));
  1695. end;
  1696. procedure ConcatLocal(p:TAsmList;const s : string);
  1697. {*********************************************************************}
  1698. { PROCEDURE ConcatLocal }
  1699. { Description: This routine emits an local definition to the }
  1700. { linked list of instructions. }
  1701. {*********************************************************************}
  1702. begin
  1703. p.concat(Tai_symbol.Createname(s,AT_LABEL,0,voidcodepointertype));
  1704. end;
  1705. end.