rautils.pas 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. {$i fpcdefs.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasmbase,aasmtai,cpubase,cpuinfo,cgbase,
  24. symconst,symbase,symtype,symdef,symsym;
  25. Const
  26. RPNMax = 10; { I think you only need 4, but just to be safe }
  27. OpMax = 25;
  28. {---------------------------------------------------------------------
  29. Local Label Management
  30. ---------------------------------------------------------------------}
  31. Type
  32. { Each local label has this structure associated with it }
  33. TLocalLabel = class(TNamedIndexItem)
  34. Emitted : boolean;
  35. constructor Create(const n:string);
  36. function Gettasmlabel:tasmlabel;
  37. private
  38. lab : tasmlabel;
  39. end;
  40. TLocalLabelList = class(TDictionary)
  41. procedure CheckEmitted;
  42. end;
  43. var
  44. LocalLabelList : TLocalLabelList;
  45. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  46. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  47. {---------------------------------------------------------------------
  48. Instruction management
  49. ---------------------------------------------------------------------}
  50. type
  51. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_LOCAL,
  52. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST,OPR_COND,OPR_REGSET);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:aint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:aint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_LOCAL : (localsym:tvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset:boolean);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (regset : tcpuregisterset);
  63. {$endif m68k}
  64. {$ifdef powerpc}
  65. OPR_COND : (cond : tasmcond);
  66. {$endif powerpc}
  67. {$ifdef arm}
  68. OPR_REGSET : (regset : tcpuregisterset);
  69. {$endif arm}
  70. end;
  71. TOperand = class
  72. hastype, { if the operand has typecasted variable }
  73. hasvar : boolean; { if the operand is loaded with a variable }
  74. size : TCGSize;
  75. opr : TOprRec;
  76. constructor create;virtual;
  77. destructor destroy;override;
  78. Procedure SetSize(_size:longint;force:boolean);virtual;
  79. Procedure SetCorrectSize(opcode:tasmop);virtual;
  80. Function SetupResult:boolean;virtual;
  81. Function SetupSelf:boolean;
  82. Function SetupOldEBP:boolean;
  83. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  84. Function SetupDirectVar(const hs:string): Boolean;
  85. Procedure InitRef;
  86. end;
  87. TCOperand = class of TOperand;
  88. TInstruction = class
  89. opcode : tasmop;
  90. condition : tasmcond;
  91. ops : byte;
  92. labeled : boolean;
  93. operands : array[1..max_operands] of toperand;
  94. constructor create(optype : tcoperand);virtual;
  95. destructor destroy;override;
  96. Procedure BuildOpcode;virtual;abstract;
  97. { converts the instruction to an instruction how it's used by the assembler writer
  98. and concats it to the passed list, the newly created item is returned }
  99. function ConcatInstruction(p:TAAsmoutput) : tai;virtual;
  100. Procedure Swapoperands;
  101. end;
  102. tstr2opentry = class(Tnamedindexitem)
  103. op: TAsmOp;
  104. end;
  105. {---------------------------------------------------------------------}
  106. { Expression parser types }
  107. {---------------------------------------------------------------------}
  108. TExprOperator = record
  109. ch: char; { operator }
  110. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  111. end;
  112. {**********************************************************************}
  113. { The following operators are supported: }
  114. { '+' : addition }
  115. { '-' : subtraction }
  116. { '*' : multiplication }
  117. { '/' : modulo division }
  118. { '^' : exclusive or }
  119. { '<' : shift left }
  120. { '>' : shift right }
  121. { '&' : bitwise and }
  122. { '|' : bitwise or }
  123. { '~' : bitwise complement }
  124. { '%' : modulo division }
  125. { nnn: longint numbers }
  126. { ( and ) parenthesis }
  127. {**********************************************************************}
  128. TExprParse = class
  129. public
  130. Constructor create;
  131. Destructor Destroy;override;
  132. Function Evaluate(Expr: String): aint;
  133. Function Priority(_Operator: Char): aint;
  134. private
  135. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  136. RPNTop : aint;
  137. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  138. OpTop : aint;
  139. Procedure RPNPush(Num: aint);
  140. Function RPNPop: aint;
  141. Procedure RPNCalc(const token: String; prefix: boolean);
  142. Procedure OpPush(_Operator: char; prefix: boolean);
  143. { In reality returns TExprOperaotr }
  144. Procedure OpPop(var _Operator:TExprOperator);
  145. end;
  146. { Evaluate an expression string to a aint }
  147. Function CalculateExpression(const expression: string): aint;
  148. {---------------------------------------------------------------------}
  149. { String routines }
  150. {---------------------------------------------------------------------}
  151. Function ValDecimal(const S:String):aint;
  152. Function ValOctal(const S:String):aint;
  153. Function ValBinary(const S:String):aint;
  154. Function ValHexaDecimal(const S:String):aint;
  155. Function PadZero(Var s: String; n: byte): Boolean;
  156. Function EscapeToPascal(const s:string): string;
  157. {---------------------------------------------------------------------
  158. Symbol helper routines
  159. ---------------------------------------------------------------------}
  160. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  161. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  162. Function SearchType(const hs:string;var size:aint): Boolean;
  163. Function SearchRecordType(const s:string): boolean;
  164. Function SearchIConstant(const s:string; var l:aint): boolean;
  165. {---------------------------------------------------------------------
  166. Instruction generation routines
  167. ---------------------------------------------------------------------}
  168. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  169. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  170. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  171. Procedure ConcatConstant(p : TAAsmoutput;value: aint; constsize:byte);
  172. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:aint);
  173. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  174. Procedure ConcatString(p : TAAsmoutput;s:string);
  175. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  176. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  177. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  178. Procedure ConcatGlobalBss(const s : string;size : aint);
  179. Procedure ConcatLocalBss(const s : string;size : aint);
  180. Implementation
  181. uses
  182. {$ifdef delphi}
  183. sysutils,
  184. {$else}
  185. strings,
  186. {$endif}
  187. defutil,systems,verbose,globals,
  188. symtable,paramgr,
  189. aasmcpu,
  190. procinfo;
  191. {*************************************************************************
  192. TExprParse
  193. *************************************************************************}
  194. Constructor TExprParse.create;
  195. Begin
  196. end;
  197. Procedure TExprParse.RPNPush(Num : aint);
  198. { Add an operand to the top of the RPN stack }
  199. begin
  200. if RPNTop < RPNMax then
  201. begin
  202. Inc(RPNTop);
  203. RPNStack[RPNTop]:=Num;
  204. end
  205. else
  206. Message(asmr_e_expr_illegal);
  207. end;
  208. Function TExprParse.RPNPop : aint;
  209. { Get the operand at the top of the RPN stack }
  210. begin
  211. if RPNTop > 0 then
  212. begin
  213. RPNPop:=RPNStack[RPNTop];
  214. Dec(RPNTop);
  215. end
  216. else
  217. Message(asmr_e_expr_illegal);
  218. end;
  219. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  220. Var
  221. Temp : aint;
  222. n1,n2 : aint;
  223. LocalError : Integer;
  224. begin
  225. { Handle operators }
  226. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  227. Case Token[1] of
  228. '+' :
  229. Begin
  230. if not prefix then
  231. RPNPush(RPNPop + RPNPop);
  232. end;
  233. '-' :
  234. Begin
  235. if prefix then
  236. RPNPush(-(RPNPop))
  237. else
  238. begin
  239. n1:=RPNPop;
  240. n2:=RPNPop;
  241. RPNPush(n2 - n1);
  242. end;
  243. end;
  244. '*' : RPNPush(RPNPop * RPNPop);
  245. '&' :
  246. begin
  247. n1:=RPNPop;
  248. n2:=RPNPop;
  249. RPNPush(n2 and n1);
  250. end;
  251. '|' :
  252. begin
  253. n1:=RPNPop;
  254. n2:=RPNPop;
  255. RPNPush(n2 or n1);
  256. end;
  257. '~' : RPNPush(NOT RPNPop);
  258. '<' :
  259. begin
  260. n1:=RPNPop;
  261. n2:=RPNPop;
  262. RPNPush(n2 SHL n1);
  263. end;
  264. '>' :
  265. begin
  266. n1:=RPNPop;
  267. n2:=RPNPop;
  268. RPNPush(n2 SHR n1);
  269. end;
  270. '%' :
  271. begin
  272. Temp:=RPNPop;
  273. if Temp <> 0 then
  274. RPNPush(RPNPop mod Temp)
  275. else
  276. begin
  277. Message(asmr_e_expr_zero_divide);
  278. { push 1 for error recovery }
  279. RPNPush(1);
  280. end;
  281. end;
  282. '^' : RPNPush(RPNPop XOR RPNPop);
  283. '/' :
  284. begin
  285. Temp:=RPNPop;
  286. if Temp <> 0 then
  287. RPNPush(RPNPop div Temp)
  288. else
  289. begin
  290. Message(asmr_e_expr_zero_divide);
  291. { push 1 for error recovery }
  292. RPNPush(1);
  293. end;
  294. end;
  295. end
  296. else
  297. begin
  298. { Convert String to number and add to stack }
  299. Val(Token, Temp, LocalError);
  300. if LocalError = 0 then
  301. RPNPush(Temp)
  302. else
  303. begin
  304. Message(asmr_e_expr_illegal);
  305. { push 1 for error recovery }
  306. RPNPush(1);
  307. end;
  308. end;
  309. end;
  310. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  311. { Add an operator onto top of the stack }
  312. begin
  313. if OpTop < OpMax then
  314. begin
  315. Inc(OpTop);
  316. OpStack[OpTop].ch:=_Operator;
  317. OpStack[OpTop].is_prefix:=prefix;
  318. end
  319. else
  320. Message(asmr_e_expr_illegal);
  321. end;
  322. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  323. { Get operator at the top of the stack }
  324. begin
  325. if OpTop > 0 then
  326. begin
  327. _Operator:=OpStack[OpTop];
  328. Dec(OpTop);
  329. end
  330. else
  331. Message(asmr_e_expr_illegal);
  332. end;
  333. Function TExprParse.Priority(_Operator : Char) : aint;
  334. { Return priority of operator }
  335. { The greater the priority, the higher the precedence }
  336. begin
  337. Case _Operator OF
  338. '(' :
  339. Priority:=0;
  340. '+', '-' :
  341. Priority:=1;
  342. '*', '/','%','<','>' :
  343. Priority:=2;
  344. '|','&','^','~' :
  345. Priority:=0;
  346. else
  347. Message(asmr_e_expr_illegal);
  348. end;
  349. end;
  350. Function TExprParse.Evaluate(Expr : String):aint;
  351. Var
  352. I : longint;
  353. Token : String;
  354. opr : TExprOperator;
  355. begin
  356. Evaluate:=0;
  357. { Reset stacks }
  358. OpTop :=0;
  359. RPNTop:=0;
  360. Token :='';
  361. { nothing to do ? }
  362. if Expr='' then
  363. exit;
  364. For I:=1 to Length(Expr) DO
  365. begin
  366. if Expr[I] in ['0'..'9'] then
  367. begin { Build multi-digit numbers }
  368. Token:=Token + Expr[I];
  369. if I = Length(Expr) then { Send last one to calculator }
  370. RPNCalc(Token,false);
  371. end
  372. else
  373. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  374. begin
  375. if Token <> '' then
  376. begin { Send last built number to calc. }
  377. RPNCalc(Token,false);
  378. Token:='';
  379. end;
  380. Case Expr[I] OF
  381. '(' : OpPush('(',false);
  382. ')' : begin
  383. While OpStack[OpTop].ch <> '(' DO
  384. Begin
  385. OpPop(opr);
  386. RPNCalc(opr.ch,opr.is_prefix);
  387. end;
  388. OpPop(opr); { Pop off and ignore the '(' }
  389. end;
  390. '+','-','~' : Begin
  391. { workaround for -2147483648 }
  392. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  393. begin
  394. token:='-';
  395. expr[i]:='+';
  396. end;
  397. { if start of expression then surely a prefix }
  398. { or if previous char was also an operator }
  399. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  400. OpPush(Expr[I],true)
  401. else
  402. Begin
  403. { Evaluate all higher priority operators }
  404. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  405. Begin
  406. OpPop(opr);
  407. RPNCalc(opr.ch,opr.is_prefix);
  408. end;
  409. OpPush(Expr[I],false);
  410. End;
  411. end;
  412. '*', '/',
  413. '^','|','&',
  414. '%','<','>' : begin
  415. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  416. Begin
  417. OpPop(opr);
  418. RPNCalc(opr.ch,opr.is_prefix);
  419. end;
  420. OpPush(Expr[I],false);
  421. end;
  422. end; { Case }
  423. end
  424. else
  425. Message(asmr_e_expr_illegal); { Handle bad input error }
  426. end;
  427. { Pop off the remaining operators }
  428. While OpTop > 0 do
  429. Begin
  430. OpPop(opr);
  431. RPNCalc(opr.ch,opr.is_prefix);
  432. end;
  433. { The result is stored on the top of the stack }
  434. Evaluate:=RPNPop;
  435. end;
  436. Destructor TExprParse.Destroy;
  437. Begin
  438. end;
  439. Function CalculateExpression(const expression: string): aint;
  440. var
  441. expr: TExprParse;
  442. Begin
  443. expr:=TExprParse.create;
  444. CalculateExpression:=expr.Evaluate(expression);
  445. expr.Free;
  446. end;
  447. {*************************************************************************}
  448. { String conversions/utils }
  449. {*************************************************************************}
  450. Function EscapeToPascal(const s:string): string;
  451. { converts a C styled string - which contains escape }
  452. { characters to a pascal style string. }
  453. var
  454. i,len : aint;
  455. hs : string;
  456. temp : string;
  457. c : char;
  458. Begin
  459. hs:='';
  460. len:=0;
  461. i:=0;
  462. while (i<length(s)) and (len<255) do
  463. begin
  464. Inc(i);
  465. if (s[i]='\') and (i<length(s)) then
  466. Begin
  467. inc(i);
  468. case s[i] of
  469. '\' :
  470. c:='\';
  471. 'b':
  472. c:=#8;
  473. 'f':
  474. c:=#12;
  475. 'n':
  476. c:=#10;
  477. 'r':
  478. c:=#13;
  479. 't':
  480. c:=#9;
  481. '"':
  482. c:='"';
  483. '0'..'7':
  484. Begin
  485. temp:=s[i];
  486. temp:=temp+s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ValOctal(temp));
  490. end;
  491. 'x':
  492. Begin
  493. temp:=s[i+1];
  494. temp:=temp+s[i+2];
  495. inc(i,2);
  496. c:=chr(ValHexaDecimal(temp));
  497. end;
  498. else
  499. Begin
  500. Message1(asmr_e_escape_seq_ignored,s[i]);
  501. c:=s[i];
  502. end;
  503. end;
  504. end
  505. else
  506. c:=s[i];
  507. inc(len);
  508. hs[len]:=c;
  509. end;
  510. hs[0]:=chr(len);
  511. EscapeToPascal:=hs;
  512. end;
  513. Function ValDecimal(const S:String):aint;
  514. { Converts a decimal string to aint }
  515. var
  516. vs : aint;
  517. c : longint;
  518. Begin
  519. vs:=0;
  520. for c:=1 to length(s) do
  521. begin
  522. vs:=vs*10;
  523. if s[c] in ['0'..'9'] then
  524. inc(vs,ord(s[c])-ord('0'))
  525. else
  526. begin
  527. Message1(asmr_e_error_converting_decimal,s);
  528. ValDecimal:=0;
  529. exit;
  530. end;
  531. end;
  532. ValDecimal:=vs;
  533. end;
  534. Function ValOctal(const S:String):aint;
  535. { Converts an octal string to aint }
  536. var
  537. vs : aint;
  538. c : longint;
  539. Begin
  540. vs:=0;
  541. for c:=1 to length(s) do
  542. begin
  543. vs:=vs shl 3;
  544. if s[c] in ['0'..'7'] then
  545. inc(vs,ord(s[c])-ord('0'))
  546. else
  547. begin
  548. Message1(asmr_e_error_converting_octal,s);
  549. ValOctal:=0;
  550. exit;
  551. end;
  552. end;
  553. ValOctal:=vs;
  554. end;
  555. Function ValBinary(const S:String):aint;
  556. { Converts a binary string to aint }
  557. var
  558. vs : aint;
  559. c : longint;
  560. Begin
  561. vs:=0;
  562. for c:=1 to length(s) do
  563. begin
  564. vs:=vs shl 1;
  565. if s[c] in ['0'..'1'] then
  566. inc(vs,ord(s[c])-ord('0'))
  567. else
  568. begin
  569. Message1(asmr_e_error_converting_binary,s);
  570. ValBinary:=0;
  571. exit;
  572. end;
  573. end;
  574. ValBinary:=vs;
  575. end;
  576. Function ValHexadecimal(const S:String):aint;
  577. { Converts a binary string to aint }
  578. var
  579. vs : aint;
  580. c : longint;
  581. Begin
  582. vs:=0;
  583. for c:=1 to length(s) do
  584. begin
  585. vs:=vs shl 4;
  586. case s[c] of
  587. '0'..'9' :
  588. inc(vs,ord(s[c])-ord('0'));
  589. 'A'..'F' :
  590. inc(vs,ord(s[c])-ord('A')+10);
  591. 'a'..'f' :
  592. inc(vs,ord(s[c])-ord('a')+10);
  593. else
  594. begin
  595. Message1(asmr_e_error_converting_hexadecimal,s);
  596. ValHexadecimal:=0;
  597. exit;
  598. end;
  599. end;
  600. end;
  601. ValHexadecimal:=vs;
  602. end;
  603. Function PadZero(Var s: String; n: byte): Boolean;
  604. Begin
  605. PadZero:=TRUE;
  606. { Do some error checking first }
  607. if Length(s) = n then
  608. exit
  609. else
  610. if Length(s) > n then
  611. Begin
  612. PadZero:=FALSE;
  613. delete(s,n+1,length(s));
  614. exit;
  615. end
  616. else
  617. PadZero:=TRUE;
  618. { Fill it up with the specified character }
  619. fillchar(s[length(s)+1],n-1,#0);
  620. s[0]:=chr(n);
  621. end;
  622. {****************************************************************************
  623. TOperand
  624. ****************************************************************************}
  625. constructor TOperand.Create;
  626. begin
  627. size:=OS_NO;
  628. hastype:=false;
  629. hasvar:=false;
  630. FillChar(Opr,sizeof(Opr),0);
  631. end;
  632. destructor TOperand.destroy;
  633. begin
  634. end;
  635. Procedure TOperand.SetSize(_size:longint;force:boolean);
  636. begin
  637. if force or
  638. ((size = OS_NO) and (_size<=extended_size)) then
  639. Begin
  640. case _size of
  641. 1 : size:=OS_8;
  642. 2 : size:=OS_16{ could be S_IS};
  643. 4 : size:=OS_32{ could be S_IL or S_FS};
  644. 8 : size:=OS_64{ could be S_D or S_FL};
  645. else
  646. begin
  647. { extended_size can also be 8, resulting in a
  648. duplicate label }
  649. if _size=extended_size then
  650. size:=OS_F80;
  651. end;
  652. end;
  653. end;
  654. end;
  655. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  656. begin
  657. end;
  658. function TOperand.SetupResult:boolean;
  659. begin
  660. SetupResult:=false;
  661. { replace by correct offset. }
  662. with current_procinfo.procdef do
  663. if (not is_void(rettype.def)) then
  664. begin
  665. if (m_tp7 in aktmodeswitches) and
  666. (not paramanager.ret_in_param(rettype.def,proccalloption)) then
  667. begin
  668. message(asmr_e_cannot_use_RESULT_here);
  669. exit;
  670. end;
  671. SetupResult:=setupvar('result',false)
  672. end
  673. else
  674. message(asmr_e_void_function);
  675. end;
  676. Function TOperand.SetupSelf:boolean;
  677. Begin
  678. SetupSelf:=false;
  679. if assigned(current_procinfo.procdef._class) then
  680. SetupSelf:=setupvar('self',false)
  681. else
  682. Message(asmr_e_cannot_use_SELF_outside_a_method);
  683. end;
  684. Function TOperand.SetupOldEBP:boolean;
  685. Begin
  686. SetupOldEBP:=false;
  687. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  688. SetupOldEBP:=setupvar('parentframe',false)
  689. else
  690. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  691. end;
  692. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  693. function symtable_has_varsyms(st:tsymtable):boolean;
  694. var
  695. sym : tsym;
  696. begin
  697. result:=false;
  698. sym:=tsym(st.symindex.first);
  699. while assigned(sym) do
  700. begin
  701. if sym.typ=varsym then
  702. begin
  703. result:=true;
  704. exit;
  705. end;
  706. sym:=tsym(sym.indexnext);
  707. end;
  708. end;
  709. procedure setconst(l:aint);
  710. begin
  711. { We return the address of the field, just like Delphi/TP }
  712. case opr.typ of
  713. OPR_NONE :
  714. begin
  715. opr.typ:=OPR_CONSTANT;
  716. opr.val:=l;
  717. end;
  718. OPR_CONSTANT :
  719. inc(opr.val,l);
  720. OPR_REFERENCE :
  721. inc(opr.ref.offset,l);
  722. OPR_LOCAL :
  723. inc(opr.localsymofs,l);
  724. else
  725. Message(asmr_e_invalid_operand_type);
  726. end;
  727. end;
  728. { search and sets up the correct fields in the Instr record }
  729. { for the NON-constant identifier passed to the routine. }
  730. { if not found returns FALSE. }
  731. var
  732. sym : tsym;
  733. srsymtable : tsymtable;
  734. harrdef : tarraydef;
  735. indexreg : tregister;
  736. l : aint;
  737. plist : psymlistitem;
  738. Begin
  739. SetupVar:=false;
  740. asmsearchsym(s,sym,srsymtable);
  741. if sym = nil then
  742. exit;
  743. if sym.typ=absolutesym then
  744. begin
  745. if (tabsolutesym(sym).abstyp=tovar) then
  746. begin
  747. { Only support simple loads }
  748. plist:=tabsolutesym(sym).ref.firstsym;
  749. if assigned(plist) and
  750. (plist^.sltype=sl_load) then
  751. sym:=plist^.sym
  752. else
  753. begin
  754. Message(asmr_e_unsupported_symbol_type);
  755. exit;
  756. end;
  757. end
  758. else
  759. begin
  760. Message(asmr_e_unsupported_symbol_type);
  761. exit;
  762. end;
  763. end;
  764. case sym.typ of
  765. varsym :
  766. begin
  767. { we always assume in asm statements that }
  768. { that the variable is valid. }
  769. tvarsym(sym).varstate:=vs_used;
  770. inc(tvarsym(sym).refs);
  771. case tvarsym(sym).owner.symtabletype of
  772. objectsymtable :
  773. begin
  774. setconst(tvarsym(sym).fieldoffset);
  775. hasvar:=true;
  776. SetupVar:=true;
  777. Exit;
  778. end;
  779. globalsymtable,
  780. staticsymtable :
  781. begin
  782. initref;
  783. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  784. end;
  785. parasymtable,
  786. localsymtable :
  787. begin
  788. if (vo_is_external in tvarsym(sym).varoptions) then
  789. begin
  790. initref;
  791. opr.ref.symbol:=objectlibrary.newasmsymbol(tvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA)
  792. end
  793. else
  794. begin
  795. if opr.typ=OPR_REFERENCE then
  796. begin
  797. indexreg:=opr.ref.base;
  798. if opr.ref.index<>NR_NO then
  799. begin
  800. if indexreg=NR_NO then
  801. indexreg:=opr.ref.index
  802. else
  803. Message(asmr_e_multiple_index);
  804. end;
  805. end
  806. else
  807. indexreg:=NR_NO;
  808. opr.typ:=OPR_LOCAL;
  809. if assigned(current_procinfo.parent) and
  810. (current_procinfo.procdef.proccalloption<>pocall_inline) and
  811. (tvarsym(sym).owner<>current_procinfo.procdef.localst) and
  812. (tvarsym(sym).owner<>current_procinfo.procdef.parast) and
  813. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  814. symtable_has_varsyms(current_procinfo.procdef.localst) then
  815. message1(asmr_e_local_para_unreachable,s);
  816. opr.localsym:=tvarsym(sym);
  817. opr.localsymofs:=0;
  818. opr.localindexreg:=indexreg;
  819. opr.localscale:=0;
  820. opr.localgetoffset:=GetOffset;
  821. end;
  822. if paramanager.push_addr_param(tvarsym(sym).varspez,tvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  823. SetSize(sizeof(aint),false);
  824. end;
  825. end;
  826. case tvarsym(sym).vartype.def.deftype of
  827. orddef,
  828. enumdef,
  829. pointerdef,
  830. floatdef :
  831. SetSize(tvarsym(sym).getsize,false);
  832. arraydef :
  833. begin
  834. { for arrays try to get the element size, take care of
  835. multiple indexes }
  836. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  837. while assigned(harrdef.elementtype.def) and
  838. (harrdef.elementtype.def.deftype=arraydef) do
  839. harrdef:=tarraydef(harrdef.elementtype.def);
  840. SetSize(harrdef.elesize,false);
  841. end;
  842. end;
  843. hasvar:=true;
  844. SetupVar:=true;
  845. Exit;
  846. end;
  847. typedconstsym :
  848. begin
  849. initref;
  850. opr.ref.symbol:=objectlibrary.newasmsymbol(ttypedconstsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  851. case ttypedconstsym(sym).typedconsttype.def.deftype of
  852. orddef,
  853. enumdef,
  854. pointerdef,
  855. floatdef :
  856. SetSize(ttypedconstsym(sym).getsize,false);
  857. arraydef :
  858. begin
  859. { for arrays try to get the element size, take care of
  860. multiple indexes }
  861. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  862. while assigned(harrdef.elementtype.def) and
  863. (harrdef.elementtype.def.deftype=arraydef) do
  864. harrdef:=tarraydef(harrdef.elementtype.def);
  865. SetSize(harrdef.elesize,false);
  866. end;
  867. end;
  868. hasvar:=true;
  869. SetupVar:=true;
  870. Exit;
  871. end;
  872. constsym :
  873. begin
  874. if tconstsym(sym).consttyp=constord then
  875. begin
  876. setconst(tconstsym(sym).value.valueord);
  877. SetupVar:=true;
  878. Exit;
  879. end;
  880. end;
  881. typesym :
  882. begin
  883. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  884. begin
  885. setconst(0);
  886. SetupVar:=TRUE;
  887. Exit;
  888. end;
  889. end;
  890. procsym :
  891. begin
  892. if opr.typ<>OPR_NONE then
  893. Message(asmr_e_invalid_operand_type);
  894. if Tprocsym(sym).procdef_count>1 then
  895. Message(asmr_w_calling_overload_func);
  896. l:=opr.ref.offset;
  897. opr.typ:=OPR_SYMBOL;
  898. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION);
  899. opr.symofs:=l;
  900. hasvar:=true;
  901. SetupVar:=TRUE;
  902. Exit;
  903. end;
  904. else
  905. begin
  906. Message(asmr_e_unsupported_symbol_type);
  907. exit;
  908. end;
  909. end;
  910. end;
  911. { looks for internal names of variables and routines }
  912. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  913. var
  914. p : tasmsymbol;
  915. begin
  916. SetupDirectVar:=false;
  917. p:=objectlibrary.getasmsymbol(hs);
  918. if assigned(p) then
  919. begin
  920. InitRef;
  921. opr.ref.symbol:=p;
  922. hasvar:=true;
  923. SetupDirectVar:=true;
  924. end;
  925. end;
  926. procedure TOperand.InitRef;
  927. {*********************************************************************}
  928. { Description: This routine first check if the opcode is of }
  929. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  930. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  931. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  932. { to point to the default segment. }
  933. {*********************************************************************}
  934. var
  935. l : aint;
  936. reg : tregister;
  937. Begin
  938. case opr.typ of
  939. OPR_REFERENCE :
  940. exit;
  941. OPR_CONSTANT :
  942. begin
  943. l:=opr.val;
  944. opr.typ:=OPR_REFERENCE;
  945. Fillchar(opr.ref,sizeof(treference),0);
  946. opr.Ref.Offset:=l;
  947. end;
  948. OPR_NONE :
  949. begin
  950. opr.typ:=OPR_REFERENCE;
  951. Fillchar(opr.ref,sizeof(treference),0);
  952. end;
  953. OPR_REGISTER :
  954. begin
  955. reg:=opr.reg;
  956. opr.typ:=OPR_REFERENCE;
  957. Fillchar(opr.ref,sizeof(treference),0);
  958. opr.Ref.base:=reg;
  959. end;
  960. else
  961. begin
  962. Message(asmr_e_invalid_operand_type);
  963. { Recover }
  964. opr.typ:=OPR_REFERENCE;
  965. Fillchar(opr.ref,sizeof(treference),0);
  966. end;
  967. end;
  968. end;
  969. {****************************************************************************
  970. TInstruction
  971. ****************************************************************************}
  972. constructor TInstruction.create(optype : tcoperand);
  973. var
  974. i : longint;
  975. Begin
  976. { these field are set to 0 anyways by the constructor helper (FK)
  977. Opcode:=A_NONE;
  978. Condition:=C_NONE;
  979. Ops:=0;
  980. }
  981. for i:=1 to max_operands do
  982. Operands[i]:=optype.create;
  983. Labeled:=false;
  984. end;
  985. destructor TInstruction.destroy;
  986. var
  987. i : longint;
  988. Begin
  989. for i:=1 to max_operands do
  990. Operands[i].free;
  991. end;
  992. Procedure TInstruction.Swapoperands;
  993. Var
  994. p : toperand;
  995. Begin
  996. case Ops of
  997. 2 :
  998. begin
  999. p:=Operands[1];
  1000. Operands[1]:=Operands[2];
  1001. Operands[2]:=p;
  1002. end;
  1003. 3 :
  1004. begin
  1005. p:=Operands[1];
  1006. Operands[1]:=Operands[3];
  1007. Operands[3]:=p;
  1008. end;
  1009. end;
  1010. end;
  1011. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  1012. var
  1013. ai : taicpu;
  1014. i : longint;
  1015. begin
  1016. ai:=taicpu.op_none(opcode);
  1017. ai.Ops:=Ops;
  1018. ai.Allocate_oper(Ops);
  1019. for i:=1 to Ops do
  1020. with operands[i].opr do
  1021. begin
  1022. case typ of
  1023. OPR_CONSTANT :
  1024. ai.loadconst(i-1,val);
  1025. OPR_REGISTER:
  1026. ai.loadreg(i-1,reg);
  1027. OPR_SYMBOL:
  1028. ai.loadsymbol(i-1,symbol,symofs);
  1029. OPR_LOCAL :
  1030. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  1031. localscale,localgetoffset);
  1032. OPR_REFERENCE:
  1033. ai.loadref(i-1,ref);
  1034. {$ifdef ARM}
  1035. OPR_REGSET:
  1036. ai.loadregset(i-1,regset);
  1037. {$endif ARM}
  1038. end;
  1039. end;
  1040. ai.SetCondition(condition);
  1041. { Concat the opcode or give an error }
  1042. if assigned(ai) then
  1043. p.concat(ai)
  1044. else
  1045. Message(asmr_e_invalid_opcode_and_operand);
  1046. result:=ai;
  1047. end;
  1048. {***************************************************************************
  1049. TLocalLabel
  1050. ***************************************************************************}
  1051. constructor TLocalLabel.create(const n:string);
  1052. begin
  1053. inherited CreateName(n);
  1054. lab:=nil;
  1055. emitted:=false;
  1056. end;
  1057. function TLocalLabel.Gettasmlabel:tasmlabel;
  1058. begin
  1059. if not assigned(lab) then
  1060. begin
  1061. objectlibrary.getlabel(lab);
  1062. { this label is forced to be used so it's always written }
  1063. lab.increfs;
  1064. end;
  1065. Gettasmlabel:=lab;
  1066. end;
  1067. {***************************************************************************
  1068. TLocalLabelList
  1069. ***************************************************************************}
  1070. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  1071. begin
  1072. if not TLocalLabel(p).emitted then
  1073. Message1(asmr_e_unknown_label_identifier,p.name);
  1074. end;
  1075. procedure TLocalLabelList.CheckEmitted;
  1076. begin
  1077. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  1078. end;
  1079. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1080. var
  1081. lab : TLocalLabel;
  1082. Begin
  1083. CreateLocalLabel:=true;
  1084. { Check if it already is defined }
  1085. lab:=TLocalLabel(LocalLabellist.Search(s));
  1086. if not assigned(lab) then
  1087. begin
  1088. lab:=TLocalLabel.Create(s);
  1089. LocalLabellist.Insert(lab);
  1090. end;
  1091. { set emitted flag and check for dup syms }
  1092. if emit then
  1093. begin
  1094. if lab.Emitted then
  1095. begin
  1096. Message1(asmr_e_dup_local_sym,lab.Name);
  1097. CreateLocalLabel:=false;
  1098. end;
  1099. lab.Emitted:=true;
  1100. end;
  1101. hl:=lab.Gettasmlabel;
  1102. end;
  1103. {****************************************************************************
  1104. Symbol table helper routines
  1105. ****************************************************************************}
  1106. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1107. var
  1108. i : integer;
  1109. begin
  1110. i:=pos('.',s);
  1111. { allow unit.identifier }
  1112. if i>0 then
  1113. begin
  1114. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1115. if assigned(srsym) then
  1116. begin
  1117. if (srsym.typ=unitsym) and
  1118. (srsym.owner.unitid=0) then
  1119. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1120. else
  1121. srsym:=nil;
  1122. end;
  1123. end
  1124. else
  1125. searchsym(s,srsym,srsymtable);
  1126. end;
  1127. Function SearchType(const hs:string;var size:aint): Boolean;
  1128. var
  1129. srsym : tsym;
  1130. srsymtable : tsymtable;
  1131. begin
  1132. result:=false;
  1133. size:=0;
  1134. asmsearchsym(hs,srsym,srsymtable);
  1135. if assigned(srsym) and
  1136. (srsym.typ=typesym) then
  1137. begin
  1138. size:=ttypesym(srsym).restype.def.size;
  1139. result:=true;
  1140. end;
  1141. end;
  1142. Function SearchRecordType(const s:string): boolean;
  1143. var
  1144. srsym : tsym;
  1145. srsymtable : tsymtable;
  1146. Begin
  1147. SearchRecordType:=false;
  1148. { Check the constants in symtable }
  1149. asmsearchsym(s,srsym,srsymtable);
  1150. if srsym <> nil then
  1151. Begin
  1152. case srsym.typ of
  1153. typesym :
  1154. begin
  1155. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1156. begin
  1157. SearchRecordType:=true;
  1158. exit;
  1159. end;
  1160. end;
  1161. end;
  1162. end;
  1163. end;
  1164. Function SearchIConstant(const s:string; var l:aint): boolean;
  1165. {**********************************************************************}
  1166. { Description: Searches for a CONSTANT of name s in either the local }
  1167. { symbol list, then in the global symbol list, and returns the value }
  1168. { of that constant in l. Returns TRUE if successfull, if not found, }
  1169. { or if the constant is not of correct type, then returns FALSE }
  1170. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1171. { respectively. }
  1172. {**********************************************************************}
  1173. var
  1174. srsym : tsym;
  1175. srsymtable : tsymtable;
  1176. Begin
  1177. SearchIConstant:=false;
  1178. { check for TRUE or FALSE reserved words first }
  1179. if s = 'TRUE' then
  1180. Begin
  1181. SearchIConstant:=TRUE;
  1182. l:=1;
  1183. exit;
  1184. end;
  1185. if s = 'FALSE' then
  1186. Begin
  1187. SearchIConstant:=TRUE;
  1188. l:=0;
  1189. exit;
  1190. end;
  1191. { Check the constants in symtable }
  1192. asmsearchsym(s,srsym,srsymtable);
  1193. if srsym <> nil then
  1194. Begin
  1195. case srsym.typ of
  1196. constsym :
  1197. begin
  1198. if tconstsym(srsym).consttyp=constord then
  1199. Begin
  1200. l:=tconstsym(srsym).value.valueord;
  1201. SearchIConstant:=TRUE;
  1202. exit;
  1203. end;
  1204. end;
  1205. enumsym:
  1206. Begin
  1207. l:=tenumsym(srsym).value;
  1208. SearchIConstant:=TRUE;
  1209. exit;
  1210. end;
  1211. end;
  1212. end;
  1213. end;
  1214. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  1215. { search and returns the offset and size of records/objects of the base }
  1216. { with field name setup in field. }
  1217. { returns FALSE if not found. }
  1218. { used when base is a variable or a typed constant name. }
  1219. var
  1220. st : tsymtable;
  1221. harrdef : tarraydef;
  1222. sym : tsym;
  1223. srsymtable : tsymtable;
  1224. i : longint;
  1225. base : string;
  1226. Begin
  1227. GetRecordOffsetSize:=FALSE;
  1228. Offset:=0;
  1229. Size:=0;
  1230. i:=pos('.',s);
  1231. if i=0 then
  1232. i:=255;
  1233. base:=Copy(s,1,i-1);
  1234. delete(s,1,i);
  1235. if base='SELF' then
  1236. st:=current_procinfo.procdef._class.symtable
  1237. else
  1238. begin
  1239. asmsearchsym(base,sym,srsymtable);
  1240. st:=nil;
  1241. { we can start with a var,type,typedconst }
  1242. if assigned(sym) then
  1243. case sym.typ of
  1244. varsym :
  1245. with Tvarsym(sym).vartype do
  1246. case def.deftype of
  1247. recorddef :
  1248. st:=trecorddef(def).symtable;
  1249. objectdef :
  1250. st:=tobjectdef(def).symtable;
  1251. end;
  1252. typesym :
  1253. with Ttypesym(sym).restype do
  1254. case def.deftype of
  1255. recorddef :
  1256. st:=trecorddef(def).symtable;
  1257. objectdef :
  1258. st:=tobjectdef(def).symtable;
  1259. end;
  1260. typedconstsym :
  1261. with Ttypedconstsym(sym).typedconsttype do
  1262. case def.deftype of
  1263. recorddef :
  1264. st:=trecorddef(def).symtable;
  1265. objectdef :
  1266. st:=tobjectdef(def).symtable;
  1267. end;
  1268. end
  1269. else
  1270. s:='';
  1271. end;
  1272. { now walk all recordsymtables }
  1273. while assigned(st) and (s<>'') do
  1274. begin
  1275. { load next field in base }
  1276. i:=pos('.',s);
  1277. if i=0 then
  1278. i:=255;
  1279. base:=Copy(s,1,i-1);
  1280. delete(s,1,i);
  1281. if st.symtabletype=objectsymtable then
  1282. sym:=search_class_member(tobjectdef(st.defowner),base)
  1283. else
  1284. sym:=tsym(st.search(base));
  1285. if not assigned(sym) then
  1286. begin
  1287. GetRecordOffsetSize:=false;
  1288. exit;
  1289. end;
  1290. st:=nil;
  1291. case sym.typ of
  1292. varsym :
  1293. with Tvarsym(sym) do
  1294. begin
  1295. inc(Offset,fieldoffset);
  1296. size:=getsize;
  1297. with vartype do
  1298. case def.deftype of
  1299. arraydef :
  1300. begin
  1301. { for arrays try to get the element size, take care of
  1302. multiple indexes }
  1303. harrdef:=tarraydef(def);
  1304. while assigned(harrdef.elementtype.def) and
  1305. (harrdef.elementtype.def.deftype=arraydef) do
  1306. harrdef:=tarraydef(harrdef.elementtype.def);
  1307. size:=harrdef.elesize;
  1308. end;
  1309. recorddef :
  1310. st:=trecorddef(def).symtable;
  1311. objectdef :
  1312. st:=tobjectdef(def).symtable;
  1313. end;
  1314. end;
  1315. end;
  1316. end;
  1317. GetRecordOffsetSize:=(s='');
  1318. end;
  1319. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1320. var
  1321. sym : tsym;
  1322. srsymtable : tsymtable;
  1323. hs : string;
  1324. Begin
  1325. hl:=nil;
  1326. SearchLabel:=false;
  1327. { Check for pascal labels, which are case insensetive }
  1328. hs:=upper(s);
  1329. asmsearchsym(hs,sym,srsymtable);
  1330. if sym=nil then
  1331. exit;
  1332. case sym.typ of
  1333. labelsym :
  1334. begin
  1335. hl:=tlabelsym(sym).lab;
  1336. if emit then
  1337. tlabelsym(sym).defined:=true
  1338. else
  1339. tlabelsym(sym).used:=true;
  1340. SearchLabel:=true;
  1341. end;
  1342. end;
  1343. end;
  1344. {*************************************************************************}
  1345. { Instruction Generation Utilities }
  1346. {*************************************************************************}
  1347. Procedure ConcatString(p : TAAsmoutput;s:string);
  1348. {*********************************************************************}
  1349. { PROCEDURE ConcatString(s:string); }
  1350. { Description: This routine adds the character chain pointed to in }
  1351. { s to the instruction linked list. }
  1352. {*********************************************************************}
  1353. Var
  1354. pc: PChar;
  1355. Begin
  1356. getmem(pc,length(s)+1);
  1357. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1358. end;
  1359. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1360. {*********************************************************************}
  1361. { PROCEDURE ConcatPasString(s:string); }
  1362. { Description: This routine adds the character chain pointed to in }
  1363. { s to the instruction linked list, contrary to ConcatString it }
  1364. { uses a pascal style string, so it conserves null characters. }
  1365. {*********************************************************************}
  1366. Begin
  1367. p.concat(Tai_string.Create(s));
  1368. end;
  1369. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1370. {*********************************************************************}
  1371. { PROCEDURE ConcatDirect(s:string) }
  1372. { Description: This routine output the string directly to the asm }
  1373. { output, it is only sed when writing special labels in AT&T mode, }
  1374. { and should not be used without due consideration, since it may }
  1375. { cause problems. }
  1376. {*********************************************************************}
  1377. Var
  1378. pc: PChar;
  1379. Begin
  1380. getmem(pc,length(s)+1);
  1381. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1382. end;
  1383. Procedure ConcatConstant(p: TAAsmoutput; value: aint; constsize:byte);
  1384. {*********************************************************************}
  1385. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1386. { Description: This routine adds the value constant to the current }
  1387. { instruction linked list. }
  1388. { maxvalue -> indicates the size of the data to initialize: }
  1389. { $ff -> create a byte node. }
  1390. { $ffff -> create a word node. }
  1391. { $ffffffff -> create a dword node. }
  1392. {*********************************************************************}
  1393. var
  1394. rangelo,rangehi : int64;
  1395. Begin
  1396. case constsize of
  1397. 1 :
  1398. begin
  1399. p.concat(Tai_const.Create_8bit(byte(value)));
  1400. rangelo:=low(shortint);
  1401. rangehi:=high(byte);
  1402. end;
  1403. 2 :
  1404. begin
  1405. p.concat(Tai_const.Create_16bit(word(value)));
  1406. rangelo:=low(smallint);
  1407. rangehi:=high(word);
  1408. end;
  1409. 4 :
  1410. begin
  1411. p.concat(Tai_const.Create_32bit(longint(value)));
  1412. rangelo:=low(longint);
  1413. rangehi:=high(cardinal);
  1414. end;
  1415. 8 :
  1416. begin
  1417. p.concat(Tai_const.Create_64bit(int64(value)));
  1418. rangelo:=0;
  1419. rangehi:=0;
  1420. end;
  1421. else
  1422. internalerror(200405011);
  1423. end;
  1424. { check for out of bounds }
  1425. if (rangelo<>0) and
  1426. ((value>rangehi) or (value<rangelo)) then
  1427. Message(asmr_e_constant_out_of_bounds);
  1428. end;
  1429. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:aint);
  1430. begin
  1431. p.concat(Tai_const.Createname(sym,AT_DATA,l));
  1432. end;
  1433. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1434. {***********************************************************************}
  1435. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1436. { Description: This routine adds the value constant to the current }
  1437. { instruction linked list. }
  1438. { real_typ -> indicates the type of the real data to initialize: }
  1439. { s32real -> create a single node. }
  1440. { s64real -> create a double node. }
  1441. { s80real -> create an extended node. }
  1442. { s64bit -> create a comp node. }
  1443. { f32bit -> create a fixed node. (not used normally) }
  1444. {***********************************************************************}
  1445. Begin
  1446. case real_typ of
  1447. s32real : p.concat(Tai_real_32bit.Create(value));
  1448. s64real :
  1449. {$ifdef ARM}
  1450. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  1451. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1452. else
  1453. {$endif ARM}
  1454. p.concat(Tai_real_64bit.Create(value));
  1455. s80real : p.concat(Tai_real_80bit.Create(value));
  1456. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1457. end;
  1458. end;
  1459. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1460. {*********************************************************************}
  1461. { PROCEDURE ConcatLabel }
  1462. { Description: This routine either emits a label or a labeled }
  1463. { instruction to the linked list of instructions. }
  1464. {*********************************************************************}
  1465. begin
  1466. p.concat(Tai_label.Create(l));
  1467. end;
  1468. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  1469. {*********************************************************************}
  1470. { PROCEDURE ConcatPublic }
  1471. { Description: This routine emits an global definition to the }
  1472. { linked list of instructions.(used by AT&T styled asm) }
  1473. {*********************************************************************}
  1474. begin
  1475. p.concat(Tai_align.Create(l));
  1476. end;
  1477. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1478. {*********************************************************************}
  1479. { PROCEDURE ConcatPublic }
  1480. { Description: This routine emits an global definition to the }
  1481. { linked list of instructions.(used by AT&T styled asm) }
  1482. {*********************************************************************}
  1483. begin
  1484. p.concat(Tai_symbol.Createname_global(s,AT_FUNCTION,0));
  1485. end;
  1486. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1487. {*********************************************************************}
  1488. { PROCEDURE ConcatLocal }
  1489. { Description: This routine emits an local definition to the }
  1490. { linked list of instructions. }
  1491. {*********************************************************************}
  1492. begin
  1493. p.concat(Tai_symbol.Createname(s,AT_FUNCTION,0));
  1494. end;
  1495. Procedure ConcatGlobalBss(const s : string;size : aint);
  1496. {*********************************************************************}
  1497. { PROCEDURE ConcatGlobalBss }
  1498. { Description: This routine emits an global datablock to the }
  1499. { linked list of instructions. }
  1500. {*********************************************************************}
  1501. begin
  1502. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1503. end;
  1504. Procedure ConcatLocalBss(const s : string;size : aint);
  1505. {*********************************************************************}
  1506. { PROCEDURE ConcatLocalBss }
  1507. { Description: This routine emits a local datablcok to the }
  1508. { linked list of instructions. }
  1509. {*********************************************************************}
  1510. begin
  1511. bssSegment.concat(Tai_datablock.Create(s,size));
  1512. end;
  1513. end.
  1514. {
  1515. $Log$
  1516. Revision 1.87 2004-06-16 20:07:09 florian
  1517. * dwarf branch merged
  1518. Revision 1.86 2004/05/06 20:30:51 florian
  1519. * m68k compiler compilation fixed
  1520. Revision 1.85.2.7 2004/05/25 21:38:53 peter
  1521. * assembler reader/writer updates
  1522. Revision 1.85.2.6 2004/05/18 20:24:03 florian
  1523. * fixed crash with unknown symbols
  1524. Revision 1.85.2.5 2004/05/02 00:13:16 peter
  1525. * remove limit of 15 digits
  1526. Revision 1.85.2.4 2004/05/01 23:36:47 peter
  1527. * assembler reader 64bit fixes
  1528. Revision 1.85.2.3 2004/05/01 16:02:09 peter
  1529. * POINTER_SIZE replaced with sizeof(aint)
  1530. * aint,aword,tconst*int moved to globtype
  1531. Revision 1.85.2.2 2004/04/27 18:18:26 peter
  1532. * aword -> aint
  1533. Revision 1.85.2.1 2004/04/12 14:45:11 peter
  1534. * tai_const_symbol and tai_const merged
  1535. Revision 1.85 2004/03/23 22:34:49 peter
  1536. * constants ordinals now always have a type assigned
  1537. * integer constants have the smallest type, unsigned prefered over
  1538. signed
  1539. Revision 1.84 2004/03/18 11:43:57 olle
  1540. * change AT_FUNCTION to AT_DATA where appropriate
  1541. Revision 1.83 2004/03/17 22:27:41 florian
  1542. * fixed handling of doubles in a native arm compiler
  1543. * fixed handling of typed double constants on arm
  1544. Revision 1.82 2004/03/02 00:36:33 olle
  1545. * big transformation of Tai_[const_]Symbol.Create[data]name*
  1546. Revision 1.81 2004/02/21 21:04:09 daniel
  1547. * Micro-optimizations
  1548. Revision 1.80 2003/11/30 10:15:42 jonas
  1549. * fixed compilation for non-x86
  1550. Revision 1.79 2003/11/21 16:29:26 florian
  1551. * fixed reading of reg. sets in the arm assembler reader
  1552. Revision 1.78 2003/11/17 23:23:47 florian
  1553. + first part of arm assembler reader
  1554. Revision 1.77 2003/11/12 16:05:39 florian
  1555. * assembler readers OOPed
  1556. + typed currency constants
  1557. + typed 128 bit float constants if the CPU supports it
  1558. Revision 1.76 2003/10/30 19:59:00 peter
  1559. * support scalefactor for opr_local
  1560. * support reference with opr_local set, fixes tw2631
  1561. Revision 1.75 2003/10/29 16:47:18 peter
  1562. * fix field offset in reference
  1563. Revision 1.74 2003/10/29 15:40:20 peter
  1564. * support indexing and offset retrieval for locals
  1565. Revision 1.73 2003/10/28 15:36:01 peter
  1566. * absolute to object field supported, fixes tb0458
  1567. Revision 1.72 2003/10/24 17:39:03 peter
  1568. * more intel parser updates
  1569. Revision 1.71 2003/10/23 17:19:11 peter
  1570. * SearchType returns also the size
  1571. Revision 1.70 2003/10/08 19:39:58 peter
  1572. * allow access to parent locals when the currnet localst has no
  1573. varsyms
  1574. Revision 1.69 2003/10/01 20:34:49 peter
  1575. * procinfo unit contains tprocinfo
  1576. * cginfo renamed to cgbase
  1577. * moved cgmessage to verbose
  1578. * fixed ppc and sparc compiles
  1579. Revision 1.68 2003/09/25 14:57:36 peter
  1580. * fix check for unreachable locals
  1581. Revision 1.67 2003/09/23 17:56:06 peter
  1582. * locals and paras are allocated in the code generation
  1583. * tvarsym.localloc contains the location of para/local when
  1584. generating code for the current procedure
  1585. Revision 1.66 2003/09/16 16:17:01 peter
  1586. * varspez in calls to push_addr_param
  1587. Revision 1.65 2003/09/03 15:55:01 peter
  1588. * NEWRA branch merged
  1589. Revision 1.64.2.1 2003/08/27 19:55:54 peter
  1590. * first tregister patch
  1591. Revision 1.64 2003/06/13 21:19:31 peter
  1592. * current_procdef removed, use current_procinfo.procdef instead
  1593. Revision 1.63 2003/06/06 14:43:29 peter
  1594. * absolutesym support
  1595. Revision 1.62 2003/05/30 23:57:08 peter
  1596. * more sparc cleanup
  1597. * accumulator removed, splitted in function_return_reg (called) and
  1598. function_result_reg (caller)
  1599. Revision 1.61 2003/05/25 08:55:49 peter
  1600. * load result using hidden parameter
  1601. Revision 1.60 2003/05/15 18:58:53 peter
  1602. * removed selfpointer_offset, vmtpointer_offset
  1603. * tvarsym.adjusted_address
  1604. * address in localsymtable is now in the real direction
  1605. * removed some obsolete globals
  1606. Revision 1.59 2003/05/12 17:22:00 jonas
  1607. * fixed (last?) remaining -tvarsym(X).address to
  1608. tg.direction*tvarsym(X).address...
  1609. Revision 1.58 2003/04/27 11:21:34 peter
  1610. * aktprocdef renamed to current_procinfo.procdef
  1611. * procinfo renamed to current_procinfo
  1612. * procinfo will now be stored in current_module so it can be
  1613. cleaned up properly
  1614. * gen_main_procsym changed to create_main_proc and release_main_proc
  1615. to also generate a tprocinfo structure
  1616. * fixed unit implicit initfinal
  1617. Revision 1.57 2003/04/27 07:29:51 peter
  1618. * current_procinfo.procdef cleanup, current_procinfo.procdef is now always nil when parsing
  1619. a new procdef declaration
  1620. * aktprocsym removed
  1621. * lexlevel removed, use symtable.symtablelevel instead
  1622. * implicit init/final code uses the normal genentry/genexit
  1623. * funcret state checking updated for new funcret handling
  1624. Revision 1.56 2003/04/25 20:59:34 peter
  1625. * removed funcretn,funcretsym, function result is now in varsym
  1626. and aliases for result and function name are added using absolutesym
  1627. * vs_hidden parameter for funcret passed in parameter
  1628. * vs_hidden fixes
  1629. * writenode changed to printnode and released from extdebug
  1630. * -vp option added to generate a tree.log with the nodetree
  1631. * nicer printnode for statements, callnode
  1632. Revision 1.55 2003/04/06 21:11:23 olle
  1633. * changed newasmsymbol to newasmsymboldata for data symbols
  1634. Revision 1.54 2003/03/28 19:16:57 peter
  1635. * generic constructor working for i386
  1636. * remove fixed self register
  1637. * esi added as address register for i386
  1638. Revision 1.53 2003/02/19 22:00:14 daniel
  1639. * Code generator converted to new register notation
  1640. - Horribily outdated todo.txt removed
  1641. Revision 1.52 2003/01/08 18:43:56 daniel
  1642. * Tregister changed into a record
  1643. Revision 1.51 2002/12/14 15:02:03 carl
  1644. * maxoperands -> max_operands (for portability in rautils.pas)
  1645. * fix some range-check errors with loadconst
  1646. + add ncgadd unit to m68k
  1647. * some bugfix of a_param_reg with LOC_CREFERENCE
  1648. Revision 1.50 2002/11/25 17:43:23 peter
  1649. * splitted defbase in defutil,symutil,defcmp
  1650. * merged isconvertable and is_equal into compare_defs(_ext)
  1651. * made operator search faster by walking the list only once
  1652. Revision 1.49 2002/11/22 22:48:10 carl
  1653. * memory optimization with tconstsym (1.5%)
  1654. Revision 1.48 2002/11/18 17:31:59 peter
  1655. * pass proccalloption to ret_in_xxx and push_xxx functions
  1656. Revision 1.47 2002/11/15 16:29:31 peter
  1657. * made tasmsymbol.refs private (merged)
  1658. Revision 1.46 2002/09/03 16:26:27 daniel
  1659. * Make Tprocdef.defs protected
  1660. Revision 1.45 2002/08/25 19:25:20 peter
  1661. * sym.insert_in_data removed
  1662. * symtable.insertvardata/insertconstdata added
  1663. * removed insert_in_data call from symtable.insert, it needs to be
  1664. called separatly. This allows to deref the address calculation
  1665. * procedures now calculate the parast addresses after the procedure
  1666. directives are parsed. This fixes the cdecl parast problem
  1667. * push_addr_param has an extra argument that specifies if cdecl is used
  1668. or not
  1669. Revision 1.44 2002/08/17 09:23:41 florian
  1670. * first part of procinfo rewrite
  1671. Revision 1.43 2002/08/16 14:24:59 carl
  1672. * issameref() to test if two references are the same (then emit no opcodes)
  1673. + ret_in_reg to replace ret_in_acc
  1674. (fix some register allocation bugs at the same time)
  1675. + save_std_register now has an extra parameter which is the
  1676. usedinproc registers
  1677. Revision 1.42 2002/08/13 18:01:52 carl
  1678. * rename swatoperands to swapoperands
  1679. + m68k first compilable version (still needs a lot of testing):
  1680. assembler generator, system information , inline
  1681. assembler reader.
  1682. Revision 1.41 2002/08/12 15:08:40 carl
  1683. + stab register indexes for powerpc (moved from gdb to cpubase)
  1684. + tprocessor enumeration moved to cpuinfo
  1685. + linker in target_info is now a class
  1686. * many many updates for m68k (will soon start to compile)
  1687. - removed some ifdef or correct them for correct cpu
  1688. Revision 1.40 2002/08/11 14:32:27 peter
  1689. * renamed current_library to objectlibrary
  1690. Revision 1.39 2002/08/11 13:24:13 peter
  1691. * saving of asmsymbols in ppu supported
  1692. * asmsymbollist global is removed and moved into a new class
  1693. tasmlibrarydata that will hold the info of a .a file which
  1694. corresponds with a single module. Added librarydata to tmodule
  1695. to keep the library info stored for the module. In the future the
  1696. objectfiles will also be stored to the tasmlibrarydata class
  1697. * all getlabel/newasmsymbol and friends are moved to the new class
  1698. Revision 1.38 2002/07/20 11:57:57 florian
  1699. * types.pas renamed to defbase.pas because D6 contains a types
  1700. unit so this would conflicts if D6 programms are compiled
  1701. + Willamette/SSE2 instructions to assembler added
  1702. Revision 1.37 2002/07/11 14:41:28 florian
  1703. * start of the new generic parameter handling
  1704. Revision 1.36 2002/07/01 18:46:25 peter
  1705. * internal linker
  1706. * reorganized aasm layer
  1707. Revision 1.35 2002/05/18 13:34:17 peter
  1708. * readded missing revisions
  1709. Revision 1.34 2002/05/16 19:46:44 carl
  1710. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1711. + try to fix temp allocation (still in ifdef)
  1712. + generic constructor calls
  1713. + start of tassembler / tmodulebase class cleanup
  1714. Revision 1.31 2002/05/12 16:53:10 peter
  1715. * moved entry and exitcode to ncgutil and cgobj
  1716. * foreach gets extra argument for passing local data to the
  1717. iterator function
  1718. * -CR checks also class typecasts at runtime by changing them
  1719. into as
  1720. * fixed compiler to cycle with the -CR option
  1721. * fixed stabs with elf writer, finally the global variables can
  1722. be watched
  1723. * removed a lot of routines from cga unit and replaced them by
  1724. calls to cgobj
  1725. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1726. u32bit then the other is typecasted also to u32bit without giving
  1727. a rangecheck warning/error.
  1728. * fixed pascal calling method with reversing also the high tree in
  1729. the parast, detected by tcalcst3 test
  1730. Revision 1.30 2002/04/20 21:32:24 carl
  1731. + generic FPC_CHECKPOINTER
  1732. + first parameter offset in stack now portable
  1733. * rename some constants
  1734. + move some cpu stuff to other units
  1735. - remove unused constents
  1736. * fix stacksize for some targets
  1737. * fix generic size problems which depend now on EXTEND_SIZE constant
  1738. Revision 1.29 2002/04/15 19:02:35 carl
  1739. + target_info.size_of_pointer -> sizeof(aint)
  1740. Revision 1.28 2002/04/02 17:11:29 peter
  1741. * tlocation,treference update
  1742. * LOC_CONSTANT added for better constant handling
  1743. * secondadd splitted in multiple routines
  1744. * location_force_reg added for loading a location to a register
  1745. of a specified size
  1746. * secondassignment parses now first the right and then the left node
  1747. (this is compatible with Kylix). This saves a lot of push/pop especially
  1748. with string operations
  1749. * adapted some routines to use the new cg methods
  1750. Revision 1.27 2002/01/29 21:32:03 peter
  1751. * allow accessing locals in other lexlevel when the current assembler
  1752. routine doesn't have locals.
  1753. Revision 1.26 2002/01/24 18:25:50 peter
  1754. * implicit result variable generation for assembler routines
  1755. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1756. }