rautils.pas 44 KB

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