rautils.pas 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643
  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,cgutils,
  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,OPR_SHIFTEROP);
  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:tabstractnormalvarsym;localsymofs:aint;localindexreg:tregister;localscale:byte;localgetoffset,localforceref: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. OPR_SHIFTEROP : (shifterop : tshifterop);
  70. {$endif arm}
  71. end;
  72. TOperand = class
  73. hastype, { if the operand has typecasted variable }
  74. hasvar : boolean; { if the operand is loaded with a variable }
  75. size : TCGSize;
  76. opr : TOprRec;
  77. constructor create;virtual;
  78. destructor destroy;override;
  79. Procedure SetSize(_size:longint;force:boolean);virtual;
  80. Procedure SetCorrectSize(opcode:tasmop);virtual;
  81. Function SetupResult:boolean;virtual;
  82. Function SetupSelf:boolean;
  83. Function SetupOldEBP:boolean;
  84. Function SetupVar(const s:string;GetOffset : boolean): 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. { converts the instruction to an instruction how it's used by the assembler writer
  97. and concats it to the passed list, the newly created item is returned }
  98. function ConcatInstruction(p:TAAsmoutput) : tai;virtual;
  99. Procedure Swapoperands;
  100. end;
  101. tstr2opentry = class(Tnamedindexitem)
  102. op: TAsmOp;
  103. end;
  104. {---------------------------------------------------------------------}
  105. { Expression parser types }
  106. {---------------------------------------------------------------------}
  107. TExprOperator = record
  108. ch: char; { operator }
  109. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  110. end;
  111. {**********************************************************************}
  112. { The following operators are supported: }
  113. { '+' : addition }
  114. { '-' : subtraction }
  115. { '*' : multiplication }
  116. { '/' : modulo division }
  117. { '^' : exclusive or }
  118. { '<' : shift left }
  119. { '>' : shift right }
  120. { '&' : bitwise and }
  121. { '|' : bitwise or }
  122. { '~' : bitwise complement }
  123. { '%' : modulo division }
  124. { nnn: longint numbers }
  125. { ( and ) parenthesis }
  126. {**********************************************************************}
  127. TExprParse = class
  128. public
  129. Constructor create;
  130. Destructor Destroy;override;
  131. Function Evaluate(Expr: String): aint;
  132. Function Priority(_Operator: Char): aint;
  133. private
  134. RPNStack : Array[1..RPNMax] of aint; { Stack For RPN calculator }
  135. RPNTop : aint;
  136. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  137. OpTop : aint;
  138. Procedure RPNPush(Num: aint);
  139. Function RPNPop: aint;
  140. Procedure RPNCalc(const token: String; prefix: boolean);
  141. Procedure OpPush(_Operator: char; prefix: boolean);
  142. { In reality returns TExprOperaotr }
  143. Procedure OpPop(var _Operator:TExprOperator);
  144. end;
  145. { Evaluate an expression string to a aint }
  146. Function CalculateExpression(const expression: string): aint;
  147. {---------------------------------------------------------------------}
  148. { String routines }
  149. {---------------------------------------------------------------------}
  150. Function ParseVal(const S:String;base:byte):aint;
  151. Function PadZero(Var s: String; n: byte): Boolean;
  152. Function EscapeToPascal(const s:string): string;
  153. {---------------------------------------------------------------------
  154. Symbol helper routines
  155. ---------------------------------------------------------------------}
  156. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  157. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  158. Function SearchType(const hs:string;var size:aint): Boolean;
  159. Function SearchRecordType(const s:string): boolean;
  160. Function SearchIConstant(const s:string; var l:aint): boolean;
  161. {---------------------------------------------------------------------
  162. Instruction generation routines
  163. ---------------------------------------------------------------------}
  164. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  165. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  166. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  167. Procedure ConcatConstant(p : TAAsmoutput;value: aint; constsize:byte);
  168. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;symtyp:tasmsymtype;l:aint);
  169. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  170. Procedure ConcatString(p : TAAsmoutput;s:string);
  171. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  172. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  173. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  174. Procedure ConcatGlobalBss(const s : string;size : aint);
  175. Procedure ConcatLocalBss(const s : string;size : aint);
  176. Implementation
  177. uses
  178. strings,
  179. defutil,systems,verbose,globals,
  180. symtable,paramgr,
  181. aasmcpu,
  182. procinfo;
  183. {*************************************************************************
  184. TExprParse
  185. *************************************************************************}
  186. Constructor TExprParse.create;
  187. Begin
  188. end;
  189. Procedure TExprParse.RPNPush(Num : aint);
  190. { Add an operand to the top of the RPN stack }
  191. begin
  192. if RPNTop < RPNMax then
  193. begin
  194. Inc(RPNTop);
  195. RPNStack[RPNTop]:=Num;
  196. end
  197. else
  198. Message(asmr_e_expr_illegal);
  199. end;
  200. Function TExprParse.RPNPop : aint;
  201. { Get the operand at the top of the RPN stack }
  202. begin
  203. if RPNTop > 0 then
  204. begin
  205. RPNPop:=RPNStack[RPNTop];
  206. Dec(RPNTop);
  207. end
  208. else
  209. Message(asmr_e_expr_illegal);
  210. end;
  211. Procedure TExprParse.RPNCalc(const Token : String; prefix:boolean); { RPN Calculator }
  212. Var
  213. Temp : aint;
  214. n1,n2 : aint;
  215. LocalError : Integer;
  216. begin
  217. { Handle operators }
  218. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  219. Case Token[1] of
  220. '+' :
  221. Begin
  222. if not prefix then
  223. RPNPush(RPNPop + RPNPop);
  224. end;
  225. '-' :
  226. Begin
  227. if prefix then
  228. RPNPush(-(RPNPop))
  229. else
  230. begin
  231. n1:=RPNPop;
  232. n2:=RPNPop;
  233. RPNPush(n2 - n1);
  234. end;
  235. end;
  236. '*' : RPNPush(RPNPop * RPNPop);
  237. '&' :
  238. begin
  239. n1:=RPNPop;
  240. n2:=RPNPop;
  241. RPNPush(n2 and n1);
  242. end;
  243. '|' :
  244. begin
  245. n1:=RPNPop;
  246. n2:=RPNPop;
  247. RPNPush(n2 or n1);
  248. end;
  249. '~' : RPNPush(NOT RPNPop);
  250. '<' :
  251. begin
  252. n1:=RPNPop;
  253. n2:=RPNPop;
  254. RPNPush(n2 SHL n1);
  255. end;
  256. '>' :
  257. begin
  258. n1:=RPNPop;
  259. n2:=RPNPop;
  260. RPNPush(n2 SHR n1);
  261. end;
  262. '%' :
  263. begin
  264. Temp:=RPNPop;
  265. if Temp <> 0 then
  266. RPNPush(RPNPop mod Temp)
  267. else
  268. begin
  269. Message(asmr_e_expr_zero_divide);
  270. { push 1 for error recovery }
  271. RPNPush(1);
  272. end;
  273. end;
  274. '^' : RPNPush(RPNPop XOR RPNPop);
  275. '/' :
  276. begin
  277. Temp:=RPNPop;
  278. if Temp <> 0 then
  279. RPNPush(RPNPop div Temp)
  280. else
  281. begin
  282. Message(asmr_e_expr_zero_divide);
  283. { push 1 for error recovery }
  284. RPNPush(1);
  285. end;
  286. end;
  287. end
  288. else
  289. begin
  290. { Convert String to number and add to stack }
  291. Val(Token, Temp, LocalError);
  292. if LocalError = 0 then
  293. RPNPush(Temp)
  294. else
  295. begin
  296. Message(asmr_e_expr_illegal);
  297. { push 1 for error recovery }
  298. RPNPush(1);
  299. end;
  300. end;
  301. end;
  302. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  303. { Add an operator onto top of the stack }
  304. begin
  305. if OpTop < OpMax then
  306. begin
  307. Inc(OpTop);
  308. OpStack[OpTop].ch:=_Operator;
  309. OpStack[OpTop].is_prefix:=prefix;
  310. end
  311. else
  312. Message(asmr_e_expr_illegal);
  313. end;
  314. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  315. { Get operator at the top of the stack }
  316. begin
  317. if OpTop > 0 then
  318. begin
  319. _Operator:=OpStack[OpTop];
  320. Dec(OpTop);
  321. end
  322. else
  323. Message(asmr_e_expr_illegal);
  324. end;
  325. Function TExprParse.Priority(_Operator : Char) : aint;
  326. { Return priority of operator }
  327. { The greater the priority, the higher the precedence }
  328. begin
  329. Case _Operator OF
  330. '(' :
  331. Priority:=0;
  332. '+', '-' :
  333. Priority:=1;
  334. '*', '/','%','<','>' :
  335. Priority:=2;
  336. '|','&','^','~' :
  337. Priority:=0;
  338. else
  339. Message(asmr_e_expr_illegal);
  340. end;
  341. end;
  342. Function TExprParse.Evaluate(Expr : String):aint;
  343. Var
  344. I : longint;
  345. Token : String;
  346. opr : TExprOperator;
  347. begin
  348. Evaluate:=0;
  349. { Reset stacks }
  350. OpTop :=0;
  351. RPNTop:=0;
  352. Token :='';
  353. { nothing to do ? }
  354. if Expr='' then
  355. exit;
  356. For I:=1 to Length(Expr) DO
  357. begin
  358. if Expr[I] in ['0'..'9'] then
  359. begin { Build multi-digit numbers }
  360. Token:=Token + Expr[I];
  361. if I = Length(Expr) then { Send last one to calculator }
  362. RPNCalc(Token,false);
  363. end
  364. else
  365. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  366. begin
  367. if Token <> '' then
  368. begin { Send last built number to calc. }
  369. RPNCalc(Token,false);
  370. Token:='';
  371. end;
  372. Case Expr[I] OF
  373. '(' : OpPush('(',false);
  374. ')' : begin
  375. While (OpTop>0) and (OpStack[OpTop].ch <> '(') DO
  376. Begin
  377. OpPop(opr);
  378. RPNCalc(opr.ch,opr.is_prefix);
  379. end;
  380. OpPop(opr); { Pop off and ignore the '(' }
  381. end;
  382. '+','-','~' : Begin
  383. { workaround for -2147483648 }
  384. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  385. begin
  386. token:='-';
  387. expr[i]:='+';
  388. end;
  389. { if start of expression then surely a prefix }
  390. { or if previous char was also an operator }
  391. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  392. OpPush(Expr[I],true)
  393. else
  394. Begin
  395. { Evaluate all higher priority operators }
  396. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  397. Begin
  398. OpPop(opr);
  399. RPNCalc(opr.ch,opr.is_prefix);
  400. end;
  401. OpPush(Expr[I],false);
  402. End;
  403. end;
  404. '*', '/',
  405. '^','|','&',
  406. '%','<','>' : begin
  407. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  408. Begin
  409. OpPop(opr);
  410. RPNCalc(opr.ch,opr.is_prefix);
  411. end;
  412. OpPush(Expr[I],false);
  413. end;
  414. end; { Case }
  415. end
  416. else
  417. Message(asmr_e_expr_illegal); { Handle bad input error }
  418. end;
  419. { Pop off the remaining operators }
  420. While OpTop > 0 do
  421. Begin
  422. OpPop(opr);
  423. RPNCalc(opr.ch,opr.is_prefix);
  424. end;
  425. { The result is stored on the top of the stack }
  426. Evaluate:=RPNPop;
  427. end;
  428. Destructor TExprParse.Destroy;
  429. Begin
  430. end;
  431. Function CalculateExpression(const expression: string): aint;
  432. var
  433. expr: TExprParse;
  434. Begin
  435. expr:=TExprParse.create;
  436. CalculateExpression:=expr.Evaluate(expression);
  437. expr.Free;
  438. end;
  439. {*************************************************************************}
  440. { String conversions/utils }
  441. {*************************************************************************}
  442. Function EscapeToPascal(const s:string): string;
  443. { converts a C styled string - which contains escape }
  444. { characters to a pascal style string. }
  445. var
  446. i,len : aint;
  447. hs : string;
  448. temp : string;
  449. c : char;
  450. Begin
  451. hs:='';
  452. len:=0;
  453. i:=0;
  454. while (i<length(s)) and (len<255) do
  455. begin
  456. Inc(i);
  457. if (s[i]='\') and (i<length(s)) then
  458. Begin
  459. inc(i);
  460. case s[i] of
  461. '\' :
  462. c:='\';
  463. 'b':
  464. c:=#8;
  465. 'f':
  466. c:=#12;
  467. 'n':
  468. c:=#10;
  469. 'r':
  470. c:=#13;
  471. 't':
  472. c:=#9;
  473. '"':
  474. c:='"';
  475. '0'..'7':
  476. Begin
  477. temp:=s[i];
  478. temp:=temp+s[i+1];
  479. temp:=temp+s[i+2];
  480. inc(i,2);
  481. c:=chr(ParseVal(temp,8));
  482. end;
  483. 'x':
  484. Begin
  485. temp:=s[i+1];
  486. temp:=temp+s[i+2];
  487. inc(i,2);
  488. c:=chr(ParseVal(temp,16));
  489. end;
  490. else
  491. Begin
  492. Message1(asmr_e_escape_seq_ignored,s[i]);
  493. c:=s[i];
  494. end;
  495. end;
  496. end
  497. else
  498. c:=s[i];
  499. inc(len);
  500. hs[len]:=c;
  501. end;
  502. hs[0]:=chr(len);
  503. EscapeToPascal:=hs;
  504. end;
  505. Function ParseVal(const S:String;base:byte):aint;
  506. { Converts a decimal string to aint }
  507. var
  508. code : integer;
  509. errmsg : word;
  510. prefix : string[2];
  511. Begin
  512. case base of
  513. 2 :
  514. begin
  515. errmsg:=asmr_e_error_converting_binary;
  516. prefix:='%';
  517. end;
  518. 8 :
  519. begin
  520. errmsg:=asmr_e_error_converting_octal;
  521. prefix:='&';
  522. end;
  523. 10 :
  524. begin
  525. errmsg:=asmr_e_error_converting_decimal;
  526. prefix:='';
  527. end;
  528. 16 :
  529. begin
  530. errmsg:=asmr_e_error_converting_hexadecimal;
  531. prefix:='$';
  532. end;
  533. else
  534. internalerror(200501202);
  535. end;
  536. val(prefix+s,result,code);
  537. if code<>0 then
  538. begin
  539. val(prefix+s,aword(result),code);
  540. if code<>0 then
  541. begin
  542. Message1(errmsg,s);
  543. result:=0;
  544. end;
  545. end;
  546. end;
  547. Function PadZero(Var s: String; n: byte): Boolean;
  548. Begin
  549. PadZero:=TRUE;
  550. { Do some error checking first }
  551. if Length(s) = n then
  552. exit
  553. else
  554. if Length(s) > n then
  555. Begin
  556. PadZero:=FALSE;
  557. delete(s,n+1,length(s));
  558. exit;
  559. end
  560. else
  561. PadZero:=TRUE;
  562. { Fill it up with the specified character }
  563. fillchar(s[length(s)+1],n-1,#0);
  564. s[0]:=chr(n);
  565. end;
  566. {****************************************************************************
  567. TOperand
  568. ****************************************************************************}
  569. constructor TOperand.Create;
  570. begin
  571. size:=OS_NO;
  572. hastype:=false;
  573. hasvar:=false;
  574. FillChar(Opr,sizeof(Opr),0);
  575. end;
  576. destructor TOperand.destroy;
  577. begin
  578. end;
  579. Procedure TOperand.SetSize(_size:longint;force:boolean);
  580. begin
  581. if force or
  582. ((size = OS_NO) and (_size<=16)) then
  583. Begin
  584. case _size of
  585. 1 : size:=OS_8;
  586. 2 : size:=OS_16{ could be S_IS};
  587. 4 : size:=OS_32{ could be S_IL or S_FS};
  588. 8 : size:=OS_64{ could be S_D or S_FL};
  589. 10 : size:=OS_F80;
  590. 16 : size:=OS_128;
  591. end;
  592. end;
  593. end;
  594. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  595. begin
  596. end;
  597. function TOperand.SetupResult:boolean;
  598. begin
  599. SetupResult:=false;
  600. { replace by correct offset. }
  601. with current_procinfo.procdef do
  602. if (not is_void(rettype.def)) then
  603. begin
  604. if (m_tp7 in aktmodeswitches) and
  605. (not paramanager.ret_in_param(rettype.def,proccalloption)) then
  606. begin
  607. message(asmr_e_cannot_use_RESULT_here);
  608. exit;
  609. end;
  610. SetupResult:=setupvar('result',false)
  611. end
  612. else
  613. message(asmr_e_void_function);
  614. end;
  615. Function TOperand.SetupSelf:boolean;
  616. Begin
  617. SetupSelf:=false;
  618. if assigned(current_procinfo.procdef._class) then
  619. SetupSelf:=setupvar('self',false)
  620. else
  621. Message(asmr_e_cannot_use_SELF_outside_a_method);
  622. end;
  623. Function TOperand.SetupOldEBP:boolean;
  624. Begin
  625. SetupOldEBP:=false;
  626. if current_procinfo.procdef.parast.symtablelevel>normal_function_level then
  627. SetupOldEBP:=setupvar('parentframe',false)
  628. else
  629. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  630. end;
  631. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  632. function symtable_has_localvarsyms(st:tsymtable):boolean;
  633. var
  634. sym : tsym;
  635. begin
  636. result:=false;
  637. sym:=tsym(st.symindex.first);
  638. while assigned(sym) do
  639. begin
  640. if sym.typ=localvarsym then
  641. begin
  642. result:=true;
  643. exit;
  644. end;
  645. sym:=tsym(sym.indexnext);
  646. end;
  647. end;
  648. procedure setconst(l:aint);
  649. begin
  650. { We return the address of the field, just like Delphi/TP }
  651. case opr.typ of
  652. OPR_NONE :
  653. begin
  654. opr.typ:=OPR_CONSTANT;
  655. opr.val:=l;
  656. end;
  657. OPR_CONSTANT :
  658. inc(opr.val,l);
  659. OPR_REFERENCE :
  660. inc(opr.ref.offset,l);
  661. OPR_LOCAL :
  662. inc(opr.localsymofs,l);
  663. else
  664. Message(asmr_e_invalid_operand_type);
  665. end;
  666. end;
  667. { search and sets up the correct fields in the Instr record }
  668. { for the NON-constant identifier passed to the routine. }
  669. { if not found returns FALSE. }
  670. var
  671. sym : tsym;
  672. srsymtable : tsymtable;
  673. harrdef : tarraydef;
  674. indexreg : tregister;
  675. l : aint;
  676. plist : psymlistitem;
  677. Begin
  678. SetupVar:=false;
  679. asmsearchsym(s,sym,srsymtable);
  680. if sym = nil then
  681. exit;
  682. if sym.typ=absolutevarsym then
  683. begin
  684. if (tabsolutevarsym(sym).abstyp=tovar) then
  685. begin
  686. { Only support simple loads }
  687. plist:=tabsolutevarsym(sym).ref.firstsym;
  688. if assigned(plist) and
  689. (plist^.sltype=sl_load) then
  690. sym:=plist^.sym
  691. else
  692. begin
  693. Message(asmr_e_unsupported_symbol_type);
  694. exit;
  695. end;
  696. end
  697. else
  698. begin
  699. Message(asmr_e_unsupported_symbol_type);
  700. exit;
  701. end;
  702. end;
  703. case sym.typ of
  704. fieldvarsym :
  705. begin
  706. setconst(tfieldvarsym(sym).fieldoffset);
  707. hasvar:=true;
  708. SetupVar:=true;
  709. end;
  710. globalvarsym,
  711. localvarsym,
  712. paravarsym :
  713. begin
  714. { we always assume in asm statements that }
  715. { that the variable is valid. }
  716. tabstractvarsym(sym).varstate:=vs_used;
  717. inc(tabstractvarsym(sym).refs);
  718. { variable can't be placed in a register }
  719. tabstractvarsym(sym).varregable:=vr_none;
  720. case sym.owner.symtabletype of
  721. globalsymtable,
  722. staticsymtable :
  723. begin
  724. initref;
  725. opr.ref.symbol:=objectlibrary.newasmsymbol(tglobalvarsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  726. end;
  727. parasymtable,
  728. localsymtable :
  729. begin
  730. if opr.typ=OPR_REFERENCE then
  731. begin
  732. indexreg:=opr.ref.base;
  733. if opr.ref.index<>NR_NO then
  734. begin
  735. if indexreg=NR_NO then
  736. indexreg:=opr.ref.index
  737. else
  738. Message(asmr_e_multiple_index);
  739. end;
  740. end
  741. else
  742. indexreg:=NR_NO;
  743. opr.typ:=OPR_LOCAL;
  744. if assigned(current_procinfo.parent) and
  745. (current_procinfo.procdef.proccalloption<>pocall_inline) and
  746. (sym.owner<>current_procinfo.procdef.localst) and
  747. (sym.owner<>current_procinfo.procdef.parast) and
  748. (current_procinfo.procdef.localst.symtablelevel>normal_function_level) and
  749. symtable_has_localvarsyms(current_procinfo.procdef.localst) then
  750. message1(asmr_e_local_para_unreachable,s);
  751. opr.localsym:=tabstractnormalvarsym(sym);
  752. opr.localsymofs:=0;
  753. opr.localindexreg:=indexreg;
  754. opr.localscale:=0;
  755. opr.localgetoffset:=GetOffset;
  756. if paramanager.push_addr_param(tabstractvarsym(sym).varspez,tabstractvarsym(sym).vartype.def,current_procinfo.procdef.proccalloption) then
  757. SetSize(sizeof(aint),false);
  758. end;
  759. end;
  760. case tabstractvarsym(sym).vartype.def.deftype of
  761. orddef,
  762. enumdef,
  763. pointerdef,
  764. arraydef,
  765. floatdef :
  766. SetSize(tabstractvarsym(sym).getsize,false);
  767. { makes no sense when using sse instructions (FK)
  768. arraydef :
  769. begin
  770. { for arrays try to get the element size, take care of
  771. multiple indexes }
  772. harrdef:=tarraydef(tabstractvarsym(sym).vartype.def);
  773. while assigned(harrdef.elementtype.def) and
  774. (harrdef.elementtype.def.deftype=arraydef) do
  775. harrdef:=tarraydef(harrdef.elementtype.def);
  776. SetSize(harrdef.elesize,false);
  777. end;
  778. }
  779. end;
  780. hasvar:=true;
  781. SetupVar:=true;
  782. Exit;
  783. end;
  784. typedconstsym :
  785. begin
  786. initref;
  787. opr.ref.symbol:=objectlibrary.newasmsymbol(ttypedconstsym(sym).mangledname,AB_EXTERNAL,AT_DATA);
  788. case ttypedconstsym(sym).typedconsttype.def.deftype of
  789. orddef,
  790. enumdef,
  791. pointerdef,
  792. floatdef :
  793. SetSize(ttypedconstsym(sym).getsize,false);
  794. arraydef :
  795. begin
  796. { for arrays try to get the element size, take care of
  797. multiple indexes }
  798. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  799. while assigned(harrdef.elementtype.def) and
  800. (harrdef.elementtype.def.deftype=arraydef) do
  801. harrdef:=tarraydef(harrdef.elementtype.def);
  802. SetSize(harrdef.elesize,false);
  803. end;
  804. end;
  805. hasvar:=true;
  806. SetupVar:=true;
  807. Exit;
  808. end;
  809. constsym :
  810. begin
  811. if tconstsym(sym).consttyp=constord then
  812. begin
  813. setconst(tconstsym(sym).value.valueord);
  814. SetupVar:=true;
  815. Exit;
  816. end;
  817. end;
  818. typesym :
  819. begin
  820. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  821. begin
  822. setconst(0);
  823. SetupVar:=TRUE;
  824. Exit;
  825. end;
  826. end;
  827. procsym :
  828. begin
  829. if opr.typ<>OPR_NONE then
  830. Message(asmr_e_invalid_operand_type);
  831. if Tprocsym(sym).procdef_count>1 then
  832. Message(asmr_w_calling_overload_func);
  833. l:=opr.ref.offset;
  834. opr.typ:=OPR_SYMBOL;
  835. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname,AB_EXTERNAL,AT_FUNCTION);
  836. opr.symofs:=l;
  837. hasvar:=true;
  838. SetupVar:=TRUE;
  839. Exit;
  840. end;
  841. else
  842. begin
  843. Message(asmr_e_unsupported_symbol_type);
  844. exit;
  845. end;
  846. end;
  847. end;
  848. procedure TOperand.InitRef;
  849. {*********************************************************************}
  850. { Description: This routine first check if the opcode is of }
  851. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  852. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  853. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  854. { to point to the default segment. }
  855. {*********************************************************************}
  856. var
  857. l : aint;
  858. reg : tregister;
  859. Begin
  860. case opr.typ of
  861. OPR_REFERENCE :
  862. exit;
  863. OPR_CONSTANT :
  864. begin
  865. l:=opr.val;
  866. opr.typ:=OPR_REFERENCE;
  867. Fillchar(opr.ref,sizeof(treference),0);
  868. opr.Ref.Offset:=l;
  869. end;
  870. OPR_NONE :
  871. begin
  872. opr.typ:=OPR_REFERENCE;
  873. Fillchar(opr.ref,sizeof(treference),0);
  874. end;
  875. OPR_REGISTER :
  876. begin
  877. reg:=opr.reg;
  878. opr.typ:=OPR_REFERENCE;
  879. Fillchar(opr.ref,sizeof(treference),0);
  880. opr.Ref.base:=reg;
  881. end;
  882. else
  883. begin
  884. Message(asmr_e_invalid_operand_type);
  885. { Recover }
  886. opr.typ:=OPR_REFERENCE;
  887. Fillchar(opr.ref,sizeof(treference),0);
  888. end;
  889. end;
  890. end;
  891. {****************************************************************************
  892. TInstruction
  893. ****************************************************************************}
  894. constructor TInstruction.create(optype : tcoperand);
  895. var
  896. i : longint;
  897. Begin
  898. { these field are set to 0 anyways by the constructor helper (FK)
  899. Opcode:=A_NONE;
  900. Condition:=C_NONE;
  901. Ops:=0;
  902. }
  903. for i:=1 to max_operands do
  904. Operands[i]:=optype.create;
  905. Labeled:=false;
  906. end;
  907. destructor TInstruction.destroy;
  908. var
  909. i : longint;
  910. Begin
  911. for i:=1 to max_operands do
  912. Operands[i].free;
  913. end;
  914. Procedure TInstruction.Swapoperands;
  915. Var
  916. p : toperand;
  917. Begin
  918. case Ops of
  919. 2 :
  920. begin
  921. p:=Operands[1];
  922. Operands[1]:=Operands[2];
  923. Operands[2]:=p;
  924. end;
  925. 3 :
  926. begin
  927. p:=Operands[1];
  928. Operands[1]:=Operands[3];
  929. Operands[3]:=p;
  930. end;
  931. end;
  932. end;
  933. function TInstruction.ConcatInstruction(p:TAAsmoutput) : tai;
  934. var
  935. ai : taicpu;
  936. i : longint;
  937. begin
  938. ai:=taicpu.op_none(opcode);
  939. ai.Ops:=Ops;
  940. ai.Allocate_oper(Ops);
  941. for i:=1 to Ops do
  942. with operands[i].opr do
  943. begin
  944. case typ of
  945. OPR_CONSTANT :
  946. ai.loadconst(i-1,val);
  947. OPR_REGISTER:
  948. ai.loadreg(i-1,reg);
  949. OPR_SYMBOL:
  950. ai.loadsymbol(i-1,symbol,symofs);
  951. OPR_LOCAL :
  952. ai.loadlocal(i-1,localsym,localsymofs,localindexreg,
  953. localscale,localgetoffset,localforceref);
  954. OPR_REFERENCE:
  955. ai.loadref(i-1,ref);
  956. {$ifdef ARM}
  957. OPR_REGSET:
  958. ai.loadregset(i-1,regset);
  959. OPR_SHIFTEROP:
  960. ai.loadshifterop(i-1,shifterop);
  961. {$endif ARM}
  962. else
  963. internalerror(200501051);
  964. end;
  965. end;
  966. ai.SetCondition(condition);
  967. { Concat the opcode or give an error }
  968. if assigned(ai) then
  969. p.concat(ai)
  970. else
  971. Message(asmr_e_invalid_opcode_and_operand);
  972. result:=ai;
  973. end;
  974. {***************************************************************************
  975. TLocalLabel
  976. ***************************************************************************}
  977. constructor TLocalLabel.create(const n:string);
  978. begin
  979. inherited CreateName(n);
  980. lab:=nil;
  981. emitted:=false;
  982. end;
  983. function TLocalLabel.Gettasmlabel:tasmlabel;
  984. begin
  985. if not assigned(lab) then
  986. begin
  987. objectlibrary.getlabel(lab);
  988. { this label is forced to be used so it's always written }
  989. lab.increfs;
  990. end;
  991. Gettasmlabel:=lab;
  992. end;
  993. {***************************************************************************
  994. TLocalLabelList
  995. ***************************************************************************}
  996. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  997. begin
  998. if not TLocalLabel(p).emitted then
  999. Message1(asmr_e_unknown_label_identifier,p.name);
  1000. end;
  1001. procedure TLocalLabelList.CheckEmitted;
  1002. begin
  1003. ForEach_Static(@LocalLabelEmitted,nil)
  1004. end;
  1005. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1006. var
  1007. lab : TLocalLabel;
  1008. Begin
  1009. CreateLocalLabel:=true;
  1010. { Check if it already is defined }
  1011. lab:=TLocalLabel(LocalLabellist.Search(s));
  1012. if not assigned(lab) then
  1013. begin
  1014. lab:=TLocalLabel.Create(s);
  1015. LocalLabellist.Insert(lab);
  1016. end;
  1017. { set emitted flag and check for dup syms }
  1018. if emit then
  1019. begin
  1020. if lab.Emitted then
  1021. begin
  1022. Message1(asmr_e_dup_local_sym,lab.Name);
  1023. CreateLocalLabel:=false;
  1024. end;
  1025. lab.Emitted:=true;
  1026. end;
  1027. hl:=lab.Gettasmlabel;
  1028. end;
  1029. {****************************************************************************
  1030. Symbol table helper routines
  1031. ****************************************************************************}
  1032. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1033. var
  1034. i : integer;
  1035. begin
  1036. i:=pos('.',s);
  1037. { allow unit.identifier }
  1038. if i>0 then
  1039. begin
  1040. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1041. if assigned(srsym) then
  1042. begin
  1043. if (srsym.typ=unitsym) and
  1044. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  1045. srsym.owner.iscurrentunit then
  1046. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1047. else
  1048. srsym:=nil;
  1049. end;
  1050. end
  1051. else
  1052. searchsym(s,srsym,srsymtable);
  1053. end;
  1054. Function SearchType(const hs:string;var size:aint): Boolean;
  1055. var
  1056. srsym : tsym;
  1057. srsymtable : tsymtable;
  1058. begin
  1059. result:=false;
  1060. size:=0;
  1061. asmsearchsym(hs,srsym,srsymtable);
  1062. if assigned(srsym) and
  1063. (srsym.typ=typesym) then
  1064. begin
  1065. size:=ttypesym(srsym).restype.def.size;
  1066. result:=true;
  1067. end;
  1068. end;
  1069. Function SearchRecordType(const s:string): boolean;
  1070. var
  1071. srsym : tsym;
  1072. srsymtable : tsymtable;
  1073. Begin
  1074. SearchRecordType:=false;
  1075. { Check the constants in symtable }
  1076. asmsearchsym(s,srsym,srsymtable);
  1077. if srsym <> nil then
  1078. Begin
  1079. case srsym.typ of
  1080. typesym :
  1081. begin
  1082. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1083. begin
  1084. SearchRecordType:=true;
  1085. exit;
  1086. end;
  1087. end;
  1088. end;
  1089. end;
  1090. end;
  1091. Function SearchIConstant(const s:string; var l:aint): boolean;
  1092. {**********************************************************************}
  1093. { Description: Searches for a CONSTANT of name s in either the local }
  1094. { symbol list, then in the global symbol list, and returns the value }
  1095. { of that constant in l. Returns TRUE if successfull, if not found, }
  1096. { or if the constant is not of correct type, then returns FALSE }
  1097. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1098. { respectively. }
  1099. {**********************************************************************}
  1100. var
  1101. srsym : tsym;
  1102. srsymtable : tsymtable;
  1103. Begin
  1104. SearchIConstant:=false;
  1105. { check for TRUE or FALSE reserved words first }
  1106. if s = 'TRUE' then
  1107. Begin
  1108. SearchIConstant:=TRUE;
  1109. l:=1;
  1110. exit;
  1111. end;
  1112. if s = 'FALSE' then
  1113. Begin
  1114. SearchIConstant:=TRUE;
  1115. l:=0;
  1116. exit;
  1117. end;
  1118. { Check the constants in symtable }
  1119. asmsearchsym(s,srsym,srsymtable);
  1120. if srsym <> nil then
  1121. Begin
  1122. case srsym.typ of
  1123. constsym :
  1124. begin
  1125. if tconstsym(srsym).consttyp=constord then
  1126. Begin
  1127. l:=tconstsym(srsym).value.valueord;
  1128. SearchIConstant:=TRUE;
  1129. exit;
  1130. end;
  1131. end;
  1132. enumsym:
  1133. Begin
  1134. l:=tenumsym(srsym).value;
  1135. SearchIConstant:=TRUE;
  1136. exit;
  1137. end;
  1138. end;
  1139. end;
  1140. end;
  1141. Function GetRecordOffsetSize(s:string;Var Offset: aint;var Size:aint):boolean;
  1142. { search and returns the offset and size of records/objects of the base }
  1143. { with field name setup in field. }
  1144. { returns FALSE if not found. }
  1145. { used when base is a variable or a typed constant name. }
  1146. var
  1147. st : tsymtable;
  1148. harrdef : tarraydef;
  1149. sym : tsym;
  1150. srsymtable : tsymtable;
  1151. i : longint;
  1152. base : string;
  1153. Begin
  1154. GetRecordOffsetSize:=FALSE;
  1155. Offset:=0;
  1156. Size:=0;
  1157. i:=pos('.',s);
  1158. if i=0 then
  1159. i:=255;
  1160. base:=Copy(s,1,i-1);
  1161. delete(s,1,i);
  1162. if base='SELF' then
  1163. st:=current_procinfo.procdef._class.symtable
  1164. else
  1165. begin
  1166. asmsearchsym(base,sym,srsymtable);
  1167. st:=nil;
  1168. { we can start with a var,type,typedconst }
  1169. if assigned(sym) then
  1170. case sym.typ of
  1171. globalvarsym,
  1172. localvarsym,
  1173. paravarsym :
  1174. st:=Tabstractvarsym(sym).vartype.def.getsymtable(gs_record);
  1175. typesym :
  1176. st:=Ttypesym(sym).restype.def.getsymtable(gs_record);
  1177. typedconstsym :
  1178. st:=Ttypedconstsym(sym).typedconsttype.def.getsymtable(gs_record);
  1179. end
  1180. else
  1181. s:='';
  1182. end;
  1183. { now walk all recordsymtables }
  1184. while assigned(st) and (s<>'') do
  1185. begin
  1186. { load next field in base }
  1187. i:=pos('.',s);
  1188. if i=0 then
  1189. i:=255;
  1190. base:=Copy(s,1,i-1);
  1191. delete(s,1,i);
  1192. if st.symtabletype=objectsymtable then
  1193. sym:=search_class_member(tobjectdef(st.defowner),base)
  1194. else
  1195. sym:=tsym(st.search(base));
  1196. if not assigned(sym) then
  1197. begin
  1198. GetRecordOffsetSize:=false;
  1199. exit;
  1200. end;
  1201. st:=nil;
  1202. case sym.typ of
  1203. fieldvarsym :
  1204. with Tfieldvarsym(sym) do
  1205. begin
  1206. inc(Offset,fieldoffset);
  1207. size:=getsize;
  1208. with vartype do
  1209. case def.deftype of
  1210. arraydef :
  1211. begin
  1212. { for arrays try to get the element size, take care of
  1213. multiple indexes }
  1214. harrdef:=tarraydef(def);
  1215. while assigned(harrdef.elementtype.def) and
  1216. (harrdef.elementtype.def.deftype=arraydef) do
  1217. harrdef:=tarraydef(harrdef.elementtype.def);
  1218. size:=harrdef.elesize;
  1219. end;
  1220. recorddef :
  1221. st:=trecorddef(def).symtable;
  1222. objectdef :
  1223. st:=tobjectdef(def).symtable;
  1224. end;
  1225. end;
  1226. end;
  1227. end;
  1228. GetRecordOffsetSize:=(s='');
  1229. end;
  1230. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1231. var
  1232. sym : tsym;
  1233. srsymtable : tsymtable;
  1234. hs : string;
  1235. Begin
  1236. hl:=nil;
  1237. SearchLabel:=false;
  1238. { Check for pascal labels, which are case insensetive }
  1239. hs:=upper(s);
  1240. asmsearchsym(hs,sym,srsymtable);
  1241. if sym=nil then
  1242. exit;
  1243. case sym.typ of
  1244. labelsym :
  1245. begin
  1246. hl:=tlabelsym(sym).lab;
  1247. if emit then
  1248. tlabelsym(sym).defined:=true
  1249. else
  1250. tlabelsym(sym).used:=true;
  1251. SearchLabel:=true;
  1252. end;
  1253. end;
  1254. end;
  1255. {*************************************************************************}
  1256. { Instruction Generation Utilities }
  1257. {*************************************************************************}
  1258. Procedure ConcatString(p : TAAsmoutput;s:string);
  1259. {*********************************************************************}
  1260. { PROCEDURE ConcatString(s:string); }
  1261. { Description: This routine adds the character chain pointed to in }
  1262. { s to the instruction linked list. }
  1263. {*********************************************************************}
  1264. Var
  1265. pc: PChar;
  1266. Begin
  1267. getmem(pc,length(s)+1);
  1268. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1269. end;
  1270. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1271. {*********************************************************************}
  1272. { PROCEDURE ConcatPasString(s:string); }
  1273. { Description: This routine adds the character chain pointed to in }
  1274. { s to the instruction linked list, contrary to ConcatString it }
  1275. { uses a pascal style string, so it conserves null characters. }
  1276. {*********************************************************************}
  1277. Begin
  1278. p.concat(Tai_string.Create(s));
  1279. end;
  1280. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1281. {*********************************************************************}
  1282. { PROCEDURE ConcatDirect(s:string) }
  1283. { Description: This routine output the string directly to the asm }
  1284. { output, it is only sed when writing special labels in AT&T mode, }
  1285. { and should not be used without due consideration, since it may }
  1286. { cause problems. }
  1287. {*********************************************************************}
  1288. Var
  1289. pc: PChar;
  1290. Begin
  1291. getmem(pc,length(s)+1);
  1292. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1293. end;
  1294. Procedure ConcatConstant(p: TAAsmoutput; value: aint; constsize:byte);
  1295. {*********************************************************************}
  1296. { PROCEDURE ConcatConstant(value: aint; maxvalue: aint); }
  1297. { Description: This routine adds the value constant to the current }
  1298. { instruction linked list. }
  1299. { maxvalue -> indicates the size of the data to initialize: }
  1300. { $ff -> create a byte node. }
  1301. { $ffff -> create a word node. }
  1302. { $ffffffff -> create a dword node. }
  1303. {*********************************************************************}
  1304. var
  1305. rangelo,rangehi : int64;
  1306. Begin
  1307. case constsize of
  1308. 1 :
  1309. begin
  1310. p.concat(Tai_const.Create_8bit(byte(value)));
  1311. rangelo:=low(shortint);
  1312. rangehi:=high(byte);
  1313. end;
  1314. 2 :
  1315. begin
  1316. p.concat(Tai_const.Create_16bit(word(value)));
  1317. rangelo:=low(smallint);
  1318. rangehi:=high(word);
  1319. end;
  1320. 4 :
  1321. begin
  1322. p.concat(Tai_const.Create_32bit(longint(value)));
  1323. rangelo:=low(longint);
  1324. rangehi:=high(cardinal);
  1325. end;
  1326. 8 :
  1327. begin
  1328. p.concat(Tai_const.Create_64bit(int64(value)));
  1329. rangelo:=0;
  1330. rangehi:=0;
  1331. end;
  1332. else
  1333. internalerror(200405011);
  1334. end;
  1335. { check for out of bounds }
  1336. if (rangelo<>0) and
  1337. ((value>rangehi) or (value<rangelo)) then
  1338. Message(asmr_e_constant_out_of_bounds);
  1339. end;
  1340. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;symtyp:tasmsymtype;l:aint);
  1341. begin
  1342. p.concat(Tai_const.Createname(sym,symtyp,l));
  1343. end;
  1344. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1345. {***********************************************************************}
  1346. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1347. { Description: This routine adds the value constant to the current }
  1348. { instruction linked list. }
  1349. { real_typ -> indicates the type of the real data to initialize: }
  1350. { s32real -> create a single node. }
  1351. { s64real -> create a double node. }
  1352. { s80real -> create an extended node. }
  1353. { s64bit -> create a comp node. }
  1354. { f32bit -> create a fixed node. (not used normally) }
  1355. {***********************************************************************}
  1356. Begin
  1357. case real_typ of
  1358. s32real : p.concat(Tai_real_32bit.Create(value));
  1359. s64real :
  1360. {$ifdef ARM}
  1361. if aktfputype in [fpu_fpa,fpu_fpa10,fpu_fpa11] then
  1362. p.concat(Tai_real_64bit.Create_hiloswapped(value))
  1363. else
  1364. {$endif ARM}
  1365. p.concat(Tai_real_64bit.Create(value));
  1366. s80real : p.concat(Tai_real_80bit.Create(value));
  1367. s64comp : p.concat(Tai_comp_64bit.Create(trunc(value)));
  1368. end;
  1369. end;
  1370. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1371. {*********************************************************************}
  1372. { PROCEDURE ConcatLabel }
  1373. { Description: This routine either emits a label or a labeled }
  1374. { instruction to the linked list of instructions. }
  1375. {*********************************************************************}
  1376. begin
  1377. p.concat(Tai_label.Create(l));
  1378. end;
  1379. procedure ConcatAlign(p:TAAsmoutput;l:aint);
  1380. {*********************************************************************}
  1381. { PROCEDURE ConcatPublic }
  1382. { Description: This routine emits an global definition to the }
  1383. { linked list of instructions.(used by AT&T styled asm) }
  1384. {*********************************************************************}
  1385. begin
  1386. p.concat(Tai_align.Create(l));
  1387. end;
  1388. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1389. {*********************************************************************}
  1390. { PROCEDURE ConcatPublic }
  1391. { Description: This routine emits an global definition to the }
  1392. { linked list of instructions.(used by AT&T styled asm) }
  1393. {*********************************************************************}
  1394. begin
  1395. p.concat(Tai_symbol.Createname_global(s,AT_FUNCTION,0));
  1396. end;
  1397. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1398. {*********************************************************************}
  1399. { PROCEDURE ConcatLocal }
  1400. { Description: This routine emits an local definition to the }
  1401. { linked list of instructions. }
  1402. {*********************************************************************}
  1403. begin
  1404. p.concat(Tai_symbol.Createname(s,AT_FUNCTION,0));
  1405. end;
  1406. Procedure ConcatGlobalBss(const s : string;size : aint);
  1407. {*********************************************************************}
  1408. { PROCEDURE ConcatGlobalBss }
  1409. { Description: This routine emits an global datablock to the }
  1410. { linked list of instructions. }
  1411. {*********************************************************************}
  1412. begin
  1413. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1414. end;
  1415. Procedure ConcatLocalBss(const s : string;size : aint);
  1416. {*********************************************************************}
  1417. { PROCEDURE ConcatLocalBss }
  1418. { Description: This routine emits a local datablcok to the }
  1419. { linked list of instructions. }
  1420. {*********************************************************************}
  1421. begin
  1422. bssSegment.concat(Tai_datablock.Create(s,size));
  1423. end;
  1424. end.
  1425. {
  1426. $Log$
  1427. Revision 1.103 2005-01-31 17:07:50 peter
  1428. * fix [regpara] in intel assembler
  1429. Revision 1.102 2005/01/20 17:05:53 peter
  1430. * use val() for decoding integers
  1431. Revision 1.101 2005/01/19 22:19:41 peter
  1432. * unit mapping rewrite
  1433. * new derefmap added
  1434. Revision 1.100 2005/01/05 15:22:39 florian
  1435. * added support of shifter ops in arm inline assembler
  1436. Revision 1.99 2004/12/22 17:09:55 peter
  1437. * support sizeof()
  1438. * fix typecasting a constant like dword(4)
  1439. Revision 1.98 2004/12/12 10:50:34 florian
  1440. * fixed operand size calculation for sse operands
  1441. + all nasm assembler targets to help page output added
  1442. Revision 1.97 2004/11/29 18:50:15 peter
  1443. * os2 fixes for import
  1444. * asmsymtype support for intel reader
  1445. Revision 1.96 2004/11/21 15:35:23 peter
  1446. * float routines all use internproc and compilerproc helpers
  1447. Revision 1.95 2004/11/09 22:32:59 peter
  1448. * small m68k updates to bring it up2date
  1449. * give better error for external local variable
  1450. Revision 1.94 2004/11/08 22:09:59 peter
  1451. * tvarsym splitted
  1452. Revision 1.93 2004/10/31 21:45:03 peter
  1453. * generic tlocation
  1454. * move tlocation to cgutils
  1455. Revision 1.92 2004/10/24 11:44:28 peter
  1456. * small regvar fixes
  1457. * loadref parameter removed from concatcopy,incrrefcount,etc
  1458. Revision 1.91 2004/10/15 09:14:17 mazen
  1459. - remove $IFDEF DELPHI and related code
  1460. - remove $IFDEF FPCPROCVAR and related code
  1461. Revision 1.90 2004/10/08 17:09:43 peter
  1462. * tvarsym.varregable added, split vo_regable from varoptions
  1463. Revision 1.89 2004/09/27 15:14:34 peter
  1464. * reference local varsyms can't be regvar
  1465. Revision 1.88 2004/06/20 08:55:30 florian
  1466. * logs truncated
  1467. Revision 1.87 2004/06/16 20:07:09 florian
  1468. * dwarf branch merged
  1469. Revision 1.86 2004/05/06 20:30:51 florian
  1470. * m68k compiler compilation fixed
  1471. Revision 1.85.2.7 2004/05/25 21:38:53 peter
  1472. * assembler reader/writer updates
  1473. Revision 1.85.2.6 2004/05/18 20:24:03 florian
  1474. * fixed crash with unknown symbols
  1475. Revision 1.85.2.5 2004/05/02 00:13:16 peter
  1476. * remove limit of 15 digits
  1477. }