rautils.pas 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811
  1. {
  2. $Id$
  3. Copyright (c) 1998 Carl Eric Codere
  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. {*************************************************************************}
  20. { This unit implements some objects as well as utilities which will be }
  21. { used by all inline assembler parsers (non-processor specific). }
  22. { }
  23. { Main routines/objects herein: }
  24. { o Object TExprParse is a simple expression parser to resolve assembler }
  25. { expressions. (Based generally on some code by Thai Tran from SWAG). }
  26. { o Object TInstruction is a simple object used for instructions }
  27. { o Record TOperand is a simple record used to store information on }
  28. { each operand. }
  29. { o String conversion routines from octal,binary and hex to decimal. }
  30. { o A linked list object/record for local labels }
  31. { o Routines for retrieving symbols (local and global) }
  32. { o Object for a linked list of strings (with duplicate strings not }
  33. { allowed). }
  34. { o Non-processor dependant routines for adding instructions to the }
  35. { instruction list. }
  36. {*************************************************************************}
  37. {--------------------------------------------------------------------}
  38. { LEFT TO DO: }
  39. { o Fix the remaining bugs in the expression parser, such as with }
  40. { 4+-3 }
  41. { o Add support for local typed constants search. }
  42. { o Add support for private/protected fields in method assembler }
  43. { routines. }
  44. {--------------------------------------------------------------------}
  45. Interface
  46. Uses
  47. globtype,systems,
  48. symtable,aasm,hcodegen,verbose,globals,files,strings,
  49. cobjects,
  50. {$ifdef i386}
  51. i386;
  52. {$endif}
  53. {$ifdef m68k}
  54. m68k;
  55. {$endif}
  56. Const
  57. RPNMax = 10; { I think you only need 4, but just to be safe }
  58. OpMax = 25;
  59. maxoperands = 3; { Maximum operands for assembler instructions }
  60. Type
  61. {---------------------------------------------------------------------}
  62. { Label Management types }
  63. {---------------------------------------------------------------------}
  64. PAsmLabel = ^TAsmLabel;
  65. { Each local label has this structure associated with it }
  66. TAsmLabel = record
  67. name: PString; { pointer to a pascal string name of label }
  68. lab: PLabel; { pointer to a label as defined in FPC }
  69. emitted: boolean; { as the label itself been emitted ? }
  70. next: PAsmLabel; { next node }
  71. end;
  72. TAsmLabelList = Object
  73. public
  74. First: PAsmLabel;
  75. Constructor Init;
  76. Destructor Done;
  77. Procedure Insert(s:string; lab: PLabel; emitted: boolean);
  78. Function Search(const s: string): PAsmLabel;
  79. private
  80. Last: PAsmLabel;
  81. Function NewPasStr(s:string): PString;
  82. end;
  83. {---------------------------------------------------------------------}
  84. { Instruction management types }
  85. {---------------------------------------------------------------------}
  86. toperandtype = (OPR_NONE,OPR_REFERENCE,OPR_CONSTANT,OPR_REGISTER,OPR_LABINSTR,
  87. OPR_REGLIST,OPR_SYMBOL);
  88. { When the TReference field isintvalue = TRUE }
  89. { then offset points to an ABSOLUTE address }
  90. { otherwise isintvalue should always be false }
  91. { Special cases: }
  92. { For the M68k Target, size is UNUSED, the }
  93. { opcode determines the size of the }
  94. { instruction. }
  95. { DIVS/DIVU/MULS/MULU of the form dn,dn:dn }
  96. { is stored as three operands!! }
  97. { Each instruction operand can be of this type }
  98. TOperand = record
  99. size: topsize;
  100. opinfo: longint; { ao_xxxx flags }
  101. overriden : boolean; { indicates if the opcode has been overriden }
  102. { by a pseudo-opcode such as DWORD PTR }
  103. case operandtype:toperandtype of
  104. { the size of the opr_none field should be at least equal to each }
  105. { other field as to facilitate initialization. }
  106. OPR_NONE: (l: array[1..sizeof(treference)] of byte);
  107. OPR_REFERENCE: (ref:treference);
  108. OPR_CONSTANT: (val: longint);
  109. OPR_REGISTER: (reg:tregister);
  110. OPR_LABINSTR: (hl: plabel);
  111. { Register list such as in the movem instruction }
  112. OPR_REGLIST: (list: set of tregister);
  113. OPR_SYMBOL : (symbol:pstring);
  114. end;
  115. TInstruction = object
  116. public
  117. operands: array[1..maxoperands] of TOperand;
  118. { if numops = zero, a size may still be valid in operands[1] }
  119. { it still should be checked. }
  120. numops: byte;
  121. { set to TRUE if the instruction is labeled. }
  122. labeled: boolean;
  123. { This is used for instructions such A_CMPSB... etc, to determine }
  124. { the size of the instruction. }
  125. stropsize: topsize;
  126. procedure init;
  127. procedure done;
  128. { sets up the prefix field with the instruction pointed to in s }
  129. procedure addprefix(tok: tasmop);
  130. { sets up the instruction with the instruction pointed to in s }
  131. procedure addinstr(tok: tasmop);
  132. { get the current instruction of this object }
  133. function getinstruction: tasmop;
  134. { get the current prefix of this instruction }
  135. function getprefix: tasmop;
  136. private
  137. prefix: tasmop;
  138. instruction: tasmop;
  139. end;
  140. {---------------------------------------------------------------------}
  141. { Expression parser types }
  142. {---------------------------------------------------------------------}
  143. { expression parser error codes }
  144. texpr_error =
  145. (zero_divide, { divide by zero. }
  146. stack_overflow, { stack overflow. }
  147. stack_underflow, { stack underflow. }
  148. invalid_number, { invalid conversion }
  149. invalid_op); { invalid operator }
  150. TExprOperator = record
  151. ch: char; { operator }
  152. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  153. end;
  154. String15 = String[15];
  155. {**********************************************************************}
  156. { The following operators are supported: }
  157. { '+' : addition }
  158. { '-' : subtraction }
  159. { '*' : multiplication }
  160. { '/' : modulo division }
  161. { '^' : exclusive or }
  162. { '<' : shift left }
  163. { '>' : shift right }
  164. { '&' : bitwise and }
  165. { '|' : bitwise or }
  166. { '~' : bitwise complement }
  167. { '%' : modulo division }
  168. { nnn: longint numbers }
  169. { ( and ) parenthesis }
  170. {**********************************************************************}
  171. TExprParse = Object
  172. public
  173. Constructor Init;
  174. Destructor Done;
  175. Function Evaluate(Expr: String): longint;
  176. Procedure Error(anerror: texpr_error); virtual;
  177. Function Priority(_Operator: Char): Integer; virtual;
  178. private
  179. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  180. RPNTop : Integer;
  181. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  182. OpTop : Integer;
  183. Procedure RPNPush(Num: Longint);
  184. Function RPNPop: Longint;
  185. Procedure RPNCalc(token: String15; prefix: boolean);
  186. Procedure OpPush(_Operator: char; prefix: boolean);
  187. { In reality returns TExprOperaotr }
  188. Procedure OpPop(var _Operator:TExprOperator);
  189. end;
  190. { Evaluate an expression string to a longint }
  191. Function CalculateExpression(const expression: string): longint;
  192. {---------------------------------------------------------------------}
  193. { String routines }
  194. {---------------------------------------------------------------------}
  195. {*********************************************************************}
  196. { PROCEDURE PadZero; }
  197. { Description: Makes sure that the string specified is of the given }
  198. { length, by padding it with binary zeros, or truncating if necessary}
  199. { Remark: The return value is determined BEFORE any eventual padding.}
  200. { Return Value: TRUE = if length of string s was <= then n }
  201. { FALSE = if length of string s was > then n }
  202. {*********************************************************************}
  203. Function PadZero(Var s: String; n: byte): Boolean;
  204. { Converts an Hex digit string to a Decimal string }
  205. { Returns '' if there was an error. }
  206. Function HexToDec(const S:String): String;
  207. { Converts a binary digit string to a Decimal string }
  208. { Returns '' if there was an error. }
  209. Function BinaryToDec(const S:String): String;
  210. { Converts an octal digit string to a Decimal string }
  211. { Returns '' if there was an error. }
  212. Function OctalToDec(const S:String): String;
  213. { Converts a string containing C styled escape sequences to }
  214. { a pascal style string. }
  215. Function EscapeToPascal(const s:string): string;
  216. Procedure ConcatPasString(p : paasmoutput;s:string);
  217. { Writes the string s directly to the assembler output }
  218. Procedure ConcatDirect(p : paasmoutput;s:string);
  219. {---------------------------------------------------------------------}
  220. { Symbol helper routines }
  221. {---------------------------------------------------------------------}
  222. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  223. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  224. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  225. Function SearchIConstant(const s:string; var l:longint): boolean;
  226. Function SearchLabel(const s: string; var hl: plabel): boolean;
  227. Function CreateVarInstr(var Instr: TInstruction; const hs:string;
  228. operandnum:byte):boolean;
  229. {*********************************************************************}
  230. { FUNCTION NewPasStr(s:string): PString }
  231. { Description: This routine allocates a string on the heap and }
  232. { returns a pointer to the allocated string. }
  233. { }
  234. { Remarks: The string allocated should not be modified, since it's }
  235. { length will be less then 255. }
  236. { Remarks: It is assumed that HeapError will be called if an }
  237. { allocation fails. }
  238. {*********************************************************************}
  239. Function newpasstr(s: string): Pointer;
  240. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  241. {$ifdef i386}
  242. Procedure FWaitWarning;
  243. {$endif}
  244. {---------------------------------------------------------------------}
  245. { Instruction generation routines }
  246. {---------------------------------------------------------------------}
  247. { swaps in the case of a 2/3 operand opcode the destination and the }
  248. { source as to put it in AT&T style instruction format. }
  249. Procedure SwapOperands(Var instr: TInstruction);
  250. Procedure ConcatLabel(p : paasmoutput;op : tasmop;var l : plabel);
  251. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  252. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  253. Procedure ConcatString(p : paasmoutput;s:string);
  254. Procedure ConcatPublic(p:paasmoutput;const s : string);
  255. Procedure ConcatLocal(p:paasmoutput;const s : string);
  256. Procedure ConcatGlobalBss(const s : string;size : longint);
  257. Procedure ConcatLocalBss(const s : string;size : longint);
  258. { add to list of external labels }
  259. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  260. { add to internal list of labels }
  261. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  262. Implementation
  263. {*************************************************************************}
  264. { Expression Parser }
  265. {*************************************************************************}
  266. Constructor TExprParse.Init;
  267. Begin
  268. end;
  269. Procedure TExprParse.Error(anerror:texpr_error);
  270. var
  271. t : tmsgconst;
  272. Begin
  273. case anerror of
  274. zero_divide: t:=assem_f_ev_zero_divide;
  275. stack_overflow: t:=assem_f_ev_stack_overflow;
  276. stack_underflow: t:=assem_f_ev_stack_underflow;
  277. invalid_number: t:=assem_f_ev_invalid_number;
  278. invalid_op: t:=assem_f_ev_invalid_op;
  279. else
  280. t:=assem_f_ev_unknown;
  281. end;
  282. Message(t);
  283. end;
  284. Procedure TExprParse.RPNPush(Num : longint); { Add an operand to the top of the RPN stack }
  285. begin
  286. if RPNTop < RPNMax then
  287. begin
  288. Inc(RPNTop);
  289. RPNStack[RPNTop] := Num;
  290. end
  291. else
  292. Error(stack_overflow); { Put some error handler here }
  293. end;
  294. Function TExprParse.RPNPop : longint; { Get the operand at the top of the RPN stack }
  295. begin
  296. if RPNTop > 0 then
  297. begin
  298. RPNPop := RPNStack[RPNTop];
  299. Dec(RPNTop);
  300. end
  301. else { Put some error handler here }
  302. Error(stack_underflow);
  303. end;
  304. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  305. Var
  306. Temp : longint;
  307. LocalError : Integer;
  308. begin
  309. { Write(Token, ' '); This just outputs the RPN expression }
  310. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  311. Case Token[1] of { Handle operators }
  312. '+' : Begin
  313. if prefix then
  314. else
  315. RPNPush(RPNPop + RPNPop);
  316. end;
  317. '-' : Begin
  318. if prefix then
  319. RPNPush(-(RPNPop))
  320. else
  321. RPNPush(RPNPop - RPNPop);
  322. end;
  323. '*' : RPNPush(RPNPop * RPNPop);
  324. '&' : RPNPush(RPNPop AND RPNPop);
  325. '|' : RPNPush(RPNPop OR RPNPop);
  326. '~' : RPNPush(NOT RPNPop);
  327. '<' : RPNPush(RPNPop SHL RPNPop);
  328. '>' : RPNPush(RPNPop SHR RPNPop);
  329. '%' : begin
  330. Temp := RPNPop;
  331. if Temp <> 0 then
  332. RPNPush(RPNPop mod Temp)
  333. else Error(zero_divide); { Handle divide by zero error }
  334. end;
  335. '^' : RPNPush(RPNPop XOR RPNPop);
  336. '/' :
  337. begin
  338. Temp := RPNPop;
  339. if Temp <> 0 then
  340. RPNPush(RPNPop div Temp)
  341. else Error(zero_divide);{ Handle divide by 0 error }
  342. end;
  343. end
  344. else
  345. begin { Convert String to number and add to stack }
  346. if token='-2147483648' then
  347. begin
  348. temp:=$80000000;
  349. localerror:=0;
  350. end
  351. else
  352. Val(Token, Temp, LocalError);
  353. if LocalError = 0 then
  354. RPNPush(Temp)
  355. else Error(invalid_number);{ Handle error }
  356. end;
  357. end;
  358. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean); { Add an operator onto top of the stack }
  359. begin
  360. if OpTop < OpMax then
  361. begin
  362. Inc(OpTop);
  363. OpStack[OpTop].ch := _Operator;
  364. OpStack[OpTop].is_prefix := prefix;
  365. end
  366. else Error(stack_overflow); { Put some error handler here }
  367. end;
  368. Procedure TExprParse.OpPop(var _Operator:TExprOperator); { Get operator at the top of the stack }
  369. begin
  370. if OpTop > 0 then
  371. begin
  372. _Operator := OpStack[OpTop];
  373. Dec(OpTop);
  374. end
  375. else Error(stack_underflow); { Put some error handler here }
  376. end;
  377. Function TExprParse.Priority(_Operator : Char) : Integer; { Return priority of operator }
  378. { The greater the priority, the higher the precedence }
  379. begin
  380. Case _Operator OF
  381. '(' : Priority := 0;
  382. '+', '-' : Priority := 1;
  383. '*', '/','%','<','>' : Priority := 2;
  384. '|','&','^','~': Priority := 0;
  385. else Error(invalid_op);{ More error handling }
  386. end;
  387. end;
  388. Function TExprParse.Evaluate(Expr : String):longint;
  389. Var
  390. I : Integer;
  391. Token : String15;
  392. opr: TExprOperator;
  393. begin
  394. OpTop := 0; { Reset stacks }
  395. RPNTop := 0;
  396. Token := '';
  397. For I := 1 to Length(Expr) DO
  398. begin
  399. if Expr[I] in ['0'..'9'] then
  400. begin { Build multi-digit numbers }
  401. Token := Token + Expr[I];
  402. if I = Length(Expr) then { Send last one to calculator }
  403. RPNCalc(Token,false);
  404. end
  405. else
  406. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  407. begin
  408. if Token <> '' then
  409. begin { Send last built number to calc. }
  410. RPNCalc(Token,false);
  411. Token := '';
  412. end;
  413. Case Expr[I] OF
  414. '(' : OpPush('(',false);
  415. ')' : begin
  416. While OpStack[OpTop].ch <> '(' DO
  417. Begin
  418. OpPop(opr);
  419. RPNCalc(opr.ch,opr.is_prefix);
  420. end;
  421. OpPop(opr); { Pop off and ignore the '(' }
  422. end;
  423. '+','-','~' : Begin
  424. { workaround for -2147483648 }
  425. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  426. begin
  427. token:='-';
  428. expr[i]:='+';
  429. end;
  430. { if start of expression then surely a prefix }
  431. { or if previous char was also an operator }
  432. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  433. OpPush(Expr[I],true)
  434. else
  435. Begin
  436. { Evaluate all higher priority operators }
  437. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  438. Begin
  439. OpPop(opr);
  440. RPNCalc(opr.ch,opr.is_prefix);
  441. end;
  442. OpPush(Expr[I],false);
  443. End;
  444. end;
  445. '*', '/',
  446. '^','|','&',
  447. '%','<','>' : begin
  448. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  449. Begin
  450. OpPop(opr);
  451. RPNCalc(opr.ch,opr.is_prefix);
  452. end;
  453. OpPush(Expr[I],false);
  454. end;
  455. end; { Case }
  456. end
  457. else
  458. Error(invalid_op); { Handle bad input error }
  459. end;
  460. { Pop off the remaining operators }
  461. While OpTop > 0 do
  462. Begin
  463. OpPop(opr);
  464. RPNCalc(opr.ch,opr.is_prefix);
  465. end;
  466. { The result is stored on the top of the stack }
  467. Evaluate := RPNPop;
  468. end;
  469. Destructor TExprParse.Done;
  470. Begin
  471. end;
  472. Function CalculateExpression(const expression: string): longint;
  473. var
  474. expr: TExprParse;
  475. Begin
  476. expr.Init;
  477. CalculateExpression := expr.Evaluate(expression);
  478. expr.Done;
  479. end;
  480. {*************************************************************************}
  481. { String conversions/utils }
  482. {*************************************************************************}
  483. Function newpasstr(s: string): Pointer;
  484. Var
  485. StrPtr: PString;
  486. Begin
  487. GetMem(StrPtr, length(s)+1);
  488. Move(s,StrPtr^,length(s)+1);
  489. newpasstr:= Strptr;
  490. end;
  491. Function EscapeToPascal(const s:string): string;
  492. { converts a C styled string - which contains escape }
  493. { characters to a pascal style string. }
  494. var
  495. i,j: word;
  496. str: string;
  497. temp: string;
  498. value: byte;
  499. code: integer;
  500. Begin
  501. str:='';
  502. i:=1;
  503. j:=1;
  504. repeat
  505. if s[i] = '\' then
  506. Begin
  507. Inc(i);
  508. if i > 255 then
  509. Begin
  510. EscapeToPascal:=str;
  511. exit;
  512. end;
  513. case s[i] of
  514. '\': insert('\',str,j);
  515. 'b': insert(#08,str,j);
  516. 'f': insert(#12,str,j);
  517. 'n': insert(#10,str,j);
  518. 'r': insert(#13,str,j);
  519. 't': insert(#09,str,j);
  520. '"': insert('"',str,j);
  521. { octal number }
  522. '0'..'7': Begin
  523. temp:=s[i];
  524. temp:=temp+s[i+1];
  525. temp:=temp+s[i+2];
  526. inc(i,2);
  527. val(octaltodec(temp),value,code);
  528. if (code <> 0) then
  529. Message(assem_w_invalid_numeric);
  530. insert(chr(value),str,j);
  531. end;
  532. { hexadecimal number }
  533. 'x': Begin
  534. temp:=s[i+1];
  535. temp:=temp+s[i+2];
  536. inc(i,2);
  537. val(hextodec(temp),value,code);
  538. if (code <> 0) then
  539. Message(assem_w_invalid_numeric);
  540. insert(chr(value),str,j);
  541. end;
  542. else
  543. Begin
  544. Message1(assem_e_escape_seq_ignored,s[i]);
  545. insert(s[i],str,j);
  546. end;
  547. end; {end case }
  548. Inc(i);
  549. end
  550. else
  551. Begin
  552. Insert(s[i],str,j);
  553. Inc(i);
  554. if i > 255 then
  555. Begin
  556. EscapeToPascal:=str;
  557. exit;
  558. end;
  559. end;
  560. Inc(j);
  561. until (i > length(s)) or (j > 255);
  562. EscapeToPascal:=str;
  563. end;
  564. Function OctalToDec(const S:String): String;
  565. { Converts an octal string to a Decimal string }
  566. { Returns '' if there was an error. }
  567. var vs: longint;
  568. c: byte;
  569. st: string;
  570. Begin
  571. vs := 0;
  572. for c:=1 to length(s) do
  573. begin
  574. case s[c] of
  575. '0': vs:=vs shl 3;
  576. '1': vs:=vs shl 3+1;
  577. '2': vs:=vs shl 3+2;
  578. '3': vs:=vs shl 3+3;
  579. '4': vs:=vs shl 3+4;
  580. '5': vs:=vs shl 3+5;
  581. '6': vs:=vs shl 3+6;
  582. '7': vs:=vs shl 3+7;
  583. else
  584. begin
  585. Message(assem_f_error_converting_octal);
  586. OctalToDec := '';
  587. exit;
  588. end;
  589. end;
  590. end;
  591. str(vs,st);
  592. OctalToDec := st;
  593. end;
  594. Function BinaryToDec(const S:String): String;
  595. { Converts a binary string to a Decimal string }
  596. { Returns '' if there was an error. }
  597. var vs: longint;
  598. c: byte;
  599. st: string;
  600. Begin
  601. vs := 0;
  602. for c:=1 to length(s) do
  603. begin
  604. if s[c] = '0' then
  605. vs:=vs shl 1
  606. else
  607. if s[c]='1' then
  608. vs:=vs shl 1+1
  609. else
  610. begin
  611. Message(assem_f_error_converting_bin);
  612. BinaryToDec := '';
  613. exit;
  614. end;
  615. end;
  616. str(vs,st);
  617. BinaryToDec := st;
  618. end;
  619. Function HexToDec(const S:String): String;
  620. var vs: longint;
  621. c: byte;
  622. st: string;
  623. Begin
  624. vs := 0;
  625. for c:=1 to length(s) do
  626. begin
  627. case upcase(s[c]) of
  628. '0': vs:=vs shl 4;
  629. '1': vs:=vs shl 4+1;
  630. '2': vs:=vs shl 4+2;
  631. '3': vs:=vs shl 4+3;
  632. '4': vs:=vs shl 4+4;
  633. '5': vs:=vs shl 4+5;
  634. '6': vs:=vs shl 4+6;
  635. '7': vs:=vs shl 4+7;
  636. '8': vs:=vs shl 4+8;
  637. '9': vs:=vs shl 4+9;
  638. 'A': vs:=vs shl 4+10;
  639. 'B': vs:=vs shl 4+11;
  640. 'C': vs:=vs shl 4+12;
  641. 'D': vs:=vs shl 4+13;
  642. 'E': vs:=vs shl 4+14;
  643. 'F': vs:=vs shl 4+15;
  644. else
  645. begin
  646. Message(assem_f_error_converting_hex);
  647. HexToDec := '';
  648. exit;
  649. end;
  650. end;
  651. end;
  652. str(vs,st);
  653. HexToDec := st;
  654. end;
  655. Function PadZero(Var s: String; n: byte): Boolean;
  656. Begin
  657. PadZero := TRUE;
  658. { Do some error checking first }
  659. if Length(s) = n then
  660. exit
  661. else
  662. if Length(s) > n then
  663. Begin
  664. PadZero := FALSE;
  665. delete(s,n+1,length(s));
  666. exit;
  667. end
  668. else
  669. PadZero := TRUE;
  670. { Fill it up with the specified character }
  671. fillchar(s[length(s)+1],n-1,#0);
  672. {$ifndef TP}
  673. {$ifopt H+}
  674. setlength(s,n);
  675. {$else}
  676. s[0] := chr(n);
  677. {$endif}
  678. {$else}
  679. s[0] := chr(n);
  680. {$endif}
  681. end;
  682. {*************************************************************************}
  683. { Instruction utilities }
  684. {*************************************************************************}
  685. Procedure TInstruction.init;
  686. var
  687. k: integer;
  688. Begin
  689. numops := 0;
  690. labeled := FALSE;
  691. stropsize := S_NO;
  692. prefix := A_NONE;
  693. instruction := A_NONE;
  694. for k:=1 to maxoperands do
  695. begin
  696. operands[k].size := S_NO;
  697. operands[k].overriden := FALSE;
  698. operands[k].operandtype := OPR_NONE;
  699. { init to zeros }
  700. fillchar(operands[k].l, sizeof(operands[k].l),#0);
  701. end;
  702. end;
  703. Procedure TInstruction.addprefix(tok: tasmop);
  704. Begin
  705. if tok = A_NONE then
  706. Message(assem_e_syn_prefix_not_found);
  707. if Prefix = A_NONE then
  708. Prefix := tok
  709. else
  710. Message(assem_e_syn_try_add_more_prefix);
  711. end;
  712. Procedure TInstruction.addinstr(tok: tasmop);
  713. Begin
  714. if tok = A_NONE then
  715. Message(assem_e_syn_opcode_not_found);
  716. Instruction := tok;
  717. end;
  718. function TInstruction.getinstruction: tasmop;
  719. Begin
  720. getinstruction := Instruction;
  721. end;
  722. { get the current prefix of this instruction }
  723. function TInstruction.getprefix: tasmop;
  724. Begin
  725. getprefix := prefix;
  726. end;
  727. Procedure TInstruction.done;
  728. var
  729. k: integer;
  730. Begin
  731. for k:=1 to numops do
  732. begin
  733. if (operands[k].operandtype=OPR_REFERENCE) and
  734. assigned(operands[k].ref.symbol) then
  735. stringdispose(operands[k].ref.symbol);
  736. if (operands[k].operandtype=OPR_SYMBOL) and
  737. assigned(operands[k].symbol) then
  738. stringdispose(operands[k].symbol);
  739. end;
  740. end;
  741. {*************************************************************************}
  742. { Local label utilities }
  743. {*************************************************************************}
  744. Constructor TAsmLabelList.Init;
  745. Begin
  746. First := nil;
  747. Last := nil;
  748. end;
  749. Procedure TAsmLabelList.Insert(s:string; lab: PLabel; emitted: boolean);
  750. {*********************************************************************}
  751. { Description: Insert a node at the end of the list with lab and }
  752. { and the name in s. The name is allocated on the heap. }
  753. { Duplicates are not allowed. }
  754. { Indicate in emitted if this label itself has been emitted, or is it}
  755. { a simple labeled instruction? }
  756. {*********************************************************************}
  757. Begin
  758. if search(s) = nil then
  759. Begin
  760. if First = nil then
  761. Begin
  762. New(First);
  763. Last := First;
  764. end
  765. else
  766. Begin
  767. New(Last^.Next);
  768. Last := Last^.Next;
  769. end;
  770. Last^.name := NewPasStr(s);
  771. Last^.Lab := lab;
  772. Last^.Next := nil;
  773. Last^.emitted := emitted;
  774. end;
  775. end;
  776. Function TAsmLabelList.Search(const s: string): PAsmLabel;
  777. {*********************************************************************}
  778. { Description: This routine searches for a label named s in the }
  779. { linked list, returns a pointer to the label if found, otherwise }
  780. { returns nil. }
  781. {*********************************************************************}
  782. Var
  783. asmlab: PAsmLabel;
  784. Begin
  785. asmlab := First;
  786. if First = nil then
  787. Begin
  788. Search := nil;
  789. exit;
  790. end;
  791. While (asmlab^.name^ <> s) and (asmlab^.Next <> nil) do
  792. asmlab := asmlab^.Next;
  793. if asmlab^.name^ = s then
  794. search := asmlab
  795. else
  796. search := nil;
  797. end;
  798. Destructor TAsmLabelList.Done;
  799. {*********************************************************************}
  800. { Description: This routine takes care of deallocating all nodes }
  801. { in the linked list, as well as deallocating the string pointers }
  802. { of these nodes. }
  803. { }
  804. { Remark: The PLabel field is NOT freed, the compiler takes care of }
  805. { this. }
  806. {*********************************************************************}
  807. Var
  808. temp: PAsmLabel;
  809. temp1: PAsmLabel;
  810. Begin
  811. temp := First;
  812. while temp <> nil do
  813. Begin
  814. Freemem(Temp^.name, length(Temp^.name^)+1);
  815. Temp1 := Temp^.Next;
  816. Dispose(Temp);
  817. Temp := Temp1;
  818. { The plabel could be deleted here, but let us not do }
  819. { it, FPC will do it instead. }
  820. end;
  821. end;
  822. Function TAsmLabelList.newpasstr(s: string): PString;
  823. {*********************************************************************}
  824. { FUNCTION NewPasStr(s:string): PString }
  825. { Description: This routine allocates a string on the heap and }
  826. { returns a pointer to the allocated string. }
  827. { }
  828. { Remarks: The string allocated should not be modified, since it's }
  829. { length will be less then 255. }
  830. { Remarks: It is assumed that HeapError will be called if an }
  831. { allocation fails. }
  832. {*********************************************************************}
  833. Var
  834. StrPtr: PString;
  835. Begin
  836. GetMem(StrPtr, length(s)+1);
  837. Move(s,StrPtr^,length(s)+1);
  838. newpasstr:= Strptr;
  839. end;
  840. {*************************************************************************}
  841. { Symbol table helper routines }
  842. {*************************************************************************}
  843. Procedure SwapOperands(Var instr: TInstruction);
  844. Var
  845. tempopr: TOperand;
  846. Begin
  847. if instr.numops = 2 then
  848. Begin
  849. tempopr := instr.operands[1];
  850. instr.operands[1] := instr.operands[2];
  851. instr.operands[2] := tempopr;
  852. end
  853. else
  854. if instr.numops = 3 then
  855. Begin
  856. tempopr := instr.operands[1];
  857. instr.operands[1] := instr.operands[3];
  858. instr.operands[3] := tempopr;
  859. end;
  860. end;
  861. Function SearchIConstant(const s:string; var l:longint): boolean;
  862. {**********************************************************************}
  863. { Description: Searches for a CONSTANT of name s in either the local }
  864. { symbol list, then in the global symbol list, and returns the value }
  865. { of that constant in l. Returns TRUE if successfull, if not found, }
  866. { or if the constant is not of correct type, then returns FALSE }
  867. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  868. { respectively. }
  869. {**********************************************************************}
  870. var
  871. sym: psym;
  872. Begin
  873. SearchIConstant := FALSE;
  874. { check for TRUE or FALSE reserved words first }
  875. if s = 'TRUE' then
  876. Begin
  877. SearchIConstant := TRUE;
  878. l := 1;
  879. end
  880. else
  881. if s = 'FALSE' then
  882. Begin
  883. SearchIConstant := TRUE;
  884. l := 0;
  885. end
  886. else
  887. if assigned(aktprocsym) then
  888. Begin
  889. if assigned(aktprocsym^.definition) then
  890. Begin
  891. { Check the local constants }
  892. if assigned(aktprocsym^.definition^.localst) then
  893. sym := aktprocsym^.definition^.localst^.search(s)
  894. else
  895. sym := nil;
  896. if assigned(sym) then
  897. Begin
  898. if (sym^.typ = constsym) and
  899. (pconstsym(sym)^.consttype in [constord,constint,constchar,constbool]) then
  900. Begin
  901. l:=pconstsym(sym)^.value;
  902. SearchIConstant := TRUE;
  903. exit;
  904. end;
  905. end;
  906. end;
  907. end;
  908. { Check the global constants }
  909. getsym(s,false);
  910. if srsym <> nil then
  911. Begin
  912. case srsym^.typ of
  913. constsym :
  914. begin
  915. if (pconstsym(srsym)^.consttype in [constord,constint,constchar,constbool]) then
  916. Begin
  917. l:=pconstsym(srsym)^.value;
  918. SearchIConstant := TRUE;
  919. exit;
  920. end;
  921. end;
  922. end;
  923. end;
  924. end;
  925. Procedure SetupResult(Var Instr:TInstruction; operandnum: byte);
  926. {**********************************************************************}
  927. { Description: This routine changes the correct fields and correct }
  928. { offset in the reference, so that it points to the __RESULT or }
  929. { @Result variable (depending on the inline asm). }
  930. { Resturns a reference with all correct offset correctly set up. }
  931. { The Operand should already point to a treference on entry. }
  932. {**********************************************************************}
  933. Begin
  934. { replace by correct offset. }
  935. if assigned(procinfo.retdef) and
  936. (procinfo.retdef<>pdef(voiddef)) then
  937. begin
  938. instr.operands[operandnum].ref.offset := procinfo.retoffset;
  939. instr.operands[operandnum].ref.base := procinfo.framepointer;
  940. { always assume that the result is valid. }
  941. procinfo.funcret_is_valid:=true;
  942. end
  943. else
  944. Message(assem_e_invalid_symbol_ref);
  945. end;
  946. {$ifdef i386}
  947. Procedure FWaitWarning;
  948. begin
  949. if (target_info.target=target_i386_GO32V2) and (cs_fp_emulation in aktmoduleswitches) then
  950. Message(assem_w_fwait_emu_prob);
  951. end;
  952. {$endif i386}
  953. Procedure SetOperandSize(var instr:TInstruction;operandnum,size:longint);
  954. begin
  955. { the current size is NOT overriden if it already }
  956. { exists, such as in the case of a byte ptr, in }
  957. { front of the identifier. }
  958. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  959. Begin
  960. case size of
  961. 1: instr.operands[operandnum].size := S_B;
  962. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  963. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  964. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  965. extended_size: instr.operands[operandnum].size := S_FX;
  966. else
  967. { this is in the case where the instruction is LEA }
  968. { or something like that, in that case size is not }
  969. { important. }
  970. instr.operands[operandnum].size := S_NO;
  971. end; { end case }
  972. end;
  973. end;
  974. Function GetVarOffsetSize(const base,field:string;Var Offset: longint;var Size:longint):boolean;
  975. { search and returns the offset and size of records/objects of the base }
  976. { with field name setup in field. }
  977. { returns FALSE if not found. }
  978. { used when base is a variable or a typed constant name. }
  979. var
  980. sym:psym;
  981. p: psym;
  982. Begin
  983. GetVarOffsetSize := FALSE;
  984. Offset := 0;
  985. { local list }
  986. if assigned(aktprocsym) then
  987. begin
  988. if assigned(aktprocsym^.definition^.localst) then
  989. sym:=aktprocsym^.definition^.localst^.search(base)
  990. else
  991. sym:=nil;
  992. if assigned(sym) then
  993. begin
  994. { field of local record variable. }
  995. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  996. begin
  997. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  998. if assigned(pvarsym(p)) then
  999. Begin
  1000. Offset := pvarsym(p)^.address;
  1001. Size:=PVarsym(p)^.getsize;
  1002. GetVarOffsetSize := TRUE;
  1003. Exit;
  1004. end;
  1005. end
  1006. else
  1007. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1008. begin
  1009. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1010. begin
  1011. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1012. if assigned(pvarsym(p)) then
  1013. Begin
  1014. Offset := pvarsym(p)^.address;
  1015. Size:=PVarsym(p)^.getsize;
  1016. GetVarOffsetSize := TRUE;
  1017. Exit;
  1018. end;
  1019. end;
  1020. end;
  1021. end
  1022. else
  1023. begin
  1024. { field of local record parameter to routine. }
  1025. if assigned(aktprocsym^.definition^.parast) then
  1026. sym:=aktprocsym^.definition^.parast^.search(base)
  1027. else
  1028. sym:=nil;
  1029. if assigned(sym) then
  1030. begin
  1031. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef)
  1032. then
  1033. begin
  1034. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1035. if assigned(p) then
  1036. Begin
  1037. Offset := pvarsym(p)^.address;
  1038. Size:=PVarsym(p)^.getsize;
  1039. GetVarOffsetSize := TRUE;
  1040. Exit;
  1041. end;
  1042. end { endif }
  1043. else
  1044. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1045. begin
  1046. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1047. begin
  1048. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1049. if assigned(pvarsym(p)) then
  1050. Begin
  1051. Offset := pvarsym(p)^.address;
  1052. Size:=PVarsym(p)^.getsize;
  1053. GetVarOffsetSize := TRUE;
  1054. Exit;
  1055. end;
  1056. end;
  1057. end;
  1058. end;
  1059. end;
  1060. end; { endif assigned(aktprocsym) }
  1061. { not found.. .now look for global variables. }
  1062. getsym(base,false);
  1063. sym:=srsym;
  1064. if assigned(sym) then
  1065. Begin
  1066. { field of global record variable. }
  1067. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=recorddef) then
  1068. begin
  1069. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1070. if assigned(p) then
  1071. Begin
  1072. Offset := pvarsym(p)^.address;
  1073. Size:=PVarsym(p)^.getsize;
  1074. GetVarOffsetSize := TRUE;
  1075. Exit;
  1076. end;
  1077. end
  1078. else
  1079. { field of global record type constant. }
  1080. if (sym^.typ=typedconstsym) and (ptypedconstsym(sym)^.definition^.deftype=recorddef)
  1081. then
  1082. begin
  1083. p:=pvarsym(precdef(pvarsym(sym)^.definition)^.symtable^.search(field));
  1084. if assigned(p) then
  1085. Begin
  1086. Offset := pvarsym(p)^.address;
  1087. Size:=PVarsym(p)^.getsize;
  1088. GetVarOffsetSize := TRUE;
  1089. Exit;
  1090. end;
  1091. end
  1092. else
  1093. if (sym^.typ=varsym) and (pvarsym(sym)^.definition^.deftype=objectdef) then
  1094. begin
  1095. if assigned(pobjectdef(pvarsym(sym)^.definition)^.publicsyms) then
  1096. begin
  1097. p:=pvarsym(pobjectdef(pvarsym(sym)^.definition)^.publicsyms^.search(field));
  1098. if assigned(pvarsym(p)) then
  1099. Begin
  1100. Offset := pvarsym(p)^.address;
  1101. Size:=PVarsym(p)^.getsize;
  1102. GetVarOffsetSize := TRUE;
  1103. Exit;
  1104. end;
  1105. end;
  1106. end;
  1107. end; { end looking for global variables .. }
  1108. end;
  1109. Function GetTypeOffsetSize(const base,field: string;Var Offset: longint;var Size:longint):boolean;
  1110. { search and returns the offset of records/objects of the base }
  1111. { with field name setup in field. }
  1112. { returns 0 if not found. }
  1113. { used when base is a variable or a typed constant name. }
  1114. var
  1115. sym:psym;
  1116. p: psym;
  1117. Begin
  1118. Offset := 0;
  1119. GetTypeOffsetSize := FALSE;
  1120. { local list }
  1121. if assigned(aktprocsym) then
  1122. begin
  1123. if assigned(aktprocsym^.definition^.localst) then
  1124. sym:=aktprocsym^.definition^.localst^.search(base)
  1125. else
  1126. sym:=nil;
  1127. if assigned(sym) then
  1128. begin
  1129. { field of local record type. }
  1130. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1131. begin
  1132. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1133. if assigned(p) then
  1134. Begin
  1135. Offset := pvarsym(p)^.address;
  1136. Size:=PVarsym(p)^.getsize;
  1137. GetTypeOffsetSize := TRUE;
  1138. Exit;
  1139. end;
  1140. end;
  1141. end
  1142. else
  1143. begin
  1144. { field of local record type to routine. }
  1145. if assigned(aktprocsym^.definition^.parast) then
  1146. sym:=aktprocsym^.definition^.parast^.search(base)
  1147. else
  1148. sym:=nil;
  1149. if assigned(sym) then
  1150. begin
  1151. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef)
  1152. then
  1153. begin
  1154. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1155. if assigned(p) then
  1156. Begin
  1157. Offset := pvarsym(p)^.address;
  1158. GetTypeOffsetSize := TRUE;
  1159. Exit;
  1160. end;
  1161. end; { endif }
  1162. end; {endif }
  1163. end; { endif }
  1164. end;
  1165. { not found.. .now look for global types. }
  1166. getsym(base,false);
  1167. sym:=srsym;
  1168. if assigned(sym) then
  1169. Begin
  1170. { field of global record types. }
  1171. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=recorddef) then
  1172. begin
  1173. p:=precdef(ptypesym(sym)^.definition)^.symtable^.search(field);
  1174. if assigned(p) then
  1175. Begin
  1176. Offset := pvarsym(p)^.address;
  1177. Size:=PVarsym(p)^.getsize;
  1178. GetTypeOffsetSize := TRUE;
  1179. Exit;
  1180. end
  1181. end
  1182. else
  1183. { public field names of objects }
  1184. if (sym^.typ=typesym) and (ptypesym(sym)^.definition^.deftype=objectdef)then
  1185. begin
  1186. if assigned(pobjectdef(ptypesym(sym)^.definition)^.publicsyms) then
  1187. Begin
  1188. p:=pobjectdef(ptypesym(sym)^.definition)^.publicsyms^.search(field);
  1189. if assigned(p) then
  1190. Begin
  1191. Offset := pvarsym(p)^.address;
  1192. Size:=PVarsym(p)^.getsize;
  1193. GetTypeOffsetSize := TRUE;
  1194. Exit;
  1195. end
  1196. end;
  1197. end;
  1198. end; { end looking for global variables .. }
  1199. end;
  1200. Function CreateVarInstr(var Instr: TInstruction; const hs:string;operandnum:byte): Boolean;
  1201. { search and sets up the correct fields in the Instr record }
  1202. { for the NON-constant identifier passed to the routine. }
  1203. { if not found returns FALSE. }
  1204. var
  1205. sym : psym;
  1206. l : longint;
  1207. Begin
  1208. CreateVarInstr := FALSE;
  1209. { are we in a routine ? }
  1210. if assigned(aktprocsym) then
  1211. begin
  1212. { search the local list for the name of this variable. }
  1213. if assigned(aktprocsym^.definition^.localst) then
  1214. sym:=aktprocsym^.definition^.localst^.search(hs)
  1215. else
  1216. sym:=nil;
  1217. if assigned(sym) then
  1218. begin
  1219. case sym^.typ of
  1220. varsym : begin
  1221. { we always assume in asm statements that }
  1222. { that the variable is valid. }
  1223. pvarsym(sym)^.is_valid:=1;
  1224. if pvarsym(sym)^.owner^.symtabletype=staticsymtable then
  1225. begin
  1226. if assigned(instr.operands[operandnum].ref.symbol) then
  1227. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1228. instr.operands[operandnum].ref.symbol:=newpasstr(pvarsym(sym)^.mangledname);
  1229. end
  1230. else
  1231. begin
  1232. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1233. instr.operands[operandnum].ref.offset := -(pvarsym(sym)^.address);
  1234. end;
  1235. { the current size is NOT overriden if it already }
  1236. { exists, such as in the case of a byte ptr, in }
  1237. { front of the identifier. }
  1238. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1239. Begin
  1240. case pvarsym(sym)^.getsize of
  1241. 1: instr.operands[operandnum].size := S_B;
  1242. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1243. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1244. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1245. extended_size: instr.operands[operandnum].size := S_FX;
  1246. else
  1247. { this is in the case where the instruction is LEA }
  1248. { or something like that, in that case size is not }
  1249. { important. }
  1250. instr.operands[operandnum].size := S_NO;
  1251. end; { end case }
  1252. end;
  1253. { ok, finished for this var }
  1254. CreateVarInstr := TRUE;
  1255. Exit;
  1256. end;
  1257. typedconstsym : begin
  1258. { we always assume in asm statements that }
  1259. { that the variable is valid. }
  1260. if assigned(instr.operands[operandnum].ref.symbol) then
  1261. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1262. instr.operands[operandnum].ref.symbol:=newpasstr(pvarsym(sym)^.mangledname);
  1263. { the current size is NOT overriden if it already }
  1264. { exists, such as in the case of a byte ptr, in }
  1265. { front of the identifier. }
  1266. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1267. Begin
  1268. case ptypedconstsym(sym)^.getsize of
  1269. 1: instr.operands[operandnum].size := S_B;
  1270. 2: instr.operands[operandnum].size := S_W{ could be S_IS};
  1271. 4: instr.operands[operandnum].size := S_L{ could be S_IL or S_FS};
  1272. 8: instr.operands[operandnum].size := S_IQ{ could be S_D or S_FL};
  1273. extended_size: instr.operands[operandnum].size := S_FX;
  1274. else
  1275. instr.operands[operandnum].size := S_NO;
  1276. end; { end case }
  1277. end;
  1278. { ok, finished for this var }
  1279. CreateVarInstr := TRUE;
  1280. Exit;
  1281. end;
  1282. constsym : begin
  1283. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1284. begin
  1285. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1286. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1287. CreateVarInstr := TRUE;
  1288. Exit;
  1289. end;
  1290. end;
  1291. typesym : begin
  1292. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1293. begin
  1294. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1295. instr.operands[operandnum].val:=0;
  1296. CreateVarInstr := TRUE;
  1297. Exit;
  1298. end;
  1299. end;
  1300. procsym : begin
  1301. { free the memory before changing the symbol name. }
  1302. if assigned(instr.operands[operandnum].ref.symbol) then
  1303. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1304. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1305. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1306. CreateVarInstr := TRUE;
  1307. Exit;
  1308. end
  1309. else
  1310. begin
  1311. Message(assem_e_unsupported_symbol_type);
  1312. exit;
  1313. end;
  1314. end;
  1315. end;
  1316. { now check for parameters passed to routine }
  1317. if assigned(aktprocsym^.definition^.parast) then
  1318. sym:=aktprocsym^.definition^.parast^.search(hs)
  1319. else
  1320. sym:=nil;
  1321. if assigned(sym) then
  1322. begin
  1323. case sym^.typ of
  1324. varsym : begin
  1325. l:=pvarsym(sym)^.address;
  1326. { set offset }
  1327. inc(l,aktprocsym^.definition^.parast^.call_offset);
  1328. pvarsym(sym)^.is_valid:=1;
  1329. instr.operands[operandnum].ref.base := procinfo.framepointer;
  1330. instr.operands[operandnum].ref.offset := l;
  1331. { the current size is NOT overriden if it already }
  1332. { exists, such as in the case of a byte ptr, in }
  1333. { front of the identifier. }
  1334. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1335. Begin
  1336. case pvarsym(sym)^.getsize of
  1337. 1: instr.operands[operandnum].size := S_B;
  1338. 2: instr.operands[operandnum].size := S_W;
  1339. 4: instr.operands[operandnum].size := S_L;
  1340. 8: instr.operands[operandnum].size := S_IQ;
  1341. extended_size: instr.operands[operandnum].size := S_FX;
  1342. else
  1343. { this is in the case where the instruction is LEA }
  1344. { or something like that, in that case size is not }
  1345. { important. }
  1346. instr.operands[operandnum].size := S_NO;
  1347. end; { end case }
  1348. end; { endif }
  1349. CreateVarInstr := TRUE;
  1350. Exit;
  1351. end;
  1352. else
  1353. begin
  1354. Message(assem_e_unsupported_symbol_type);
  1355. exit;
  1356. end;
  1357. end; { case }
  1358. end; { endif }
  1359. end;
  1360. { not found.. .now look for global variables. }
  1361. getsym(hs,false);
  1362. sym:=srsym;
  1363. if assigned(sym) then
  1364. Begin
  1365. case sym^.typ of
  1366. varsym,
  1367. typedconstsym : Begin
  1368. { free the memory before changing the symbol name. }
  1369. if assigned(instr.operands[operandnum].ref.symbol) then
  1370. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1371. instr.operands[operandnum].ref.symbol:=newpasstr(sym^.mangledname);
  1372. { the current size is NOT overriden if it already }
  1373. { exists, such as in the case of a byte ptr, in }
  1374. { front of the identifier. }
  1375. if (instr.operands[operandnum].size = S_NO) or (instr.operands[operandnum].overriden = FALSE) then
  1376. Begin
  1377. case pvarsym(sym)^.getsize of
  1378. 1: instr.operands[operandnum].size := S_B;
  1379. 2: instr.operands[operandnum].size := S_W;
  1380. 4: instr.operands[operandnum].size := S_L;
  1381. 8: instr.operands[operandnum].size := S_IQ;
  1382. else
  1383. { this is in the case where the instruction is LEA }
  1384. { or something like that, in that case size is not }
  1385. { important. }
  1386. instr.operands[operandnum].size := S_NO;
  1387. end;
  1388. end
  1389. else
  1390. if (instr.operands[operandnum].size = S_NO) and (sym^.typ = typedconstsym) then
  1391. Begin
  1392. { only these are valid sizes, otherwise prefixes are }
  1393. { required. }
  1394. case ptypedconstsym(sym)^.definition^.size of
  1395. 1: instr.operands[operandnum].size := S_B;
  1396. 2: instr.operands[operandnum].size := S_W;
  1397. 4: instr.operands[operandnum].size := S_L;
  1398. 8: instr.operands[operandnum].size := S_IQ;
  1399. else
  1400. { this is in the case where the instruction is LEA }
  1401. { or something like that, in that case size is not }
  1402. { important. }
  1403. instr.operands[operandnum].size := S_NO;
  1404. end;
  1405. end; { endif }
  1406. CreateVarInstr := TRUE;
  1407. Exit;
  1408. end;
  1409. constsym : begin
  1410. if pconstsym(sym)^.consttype in [constint,constchar,constbool] then
  1411. begin
  1412. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1413. instr.operands[operandnum].val:=pconstsym(sym)^.value;
  1414. CreateVarInstr := TRUE;
  1415. Exit;
  1416. end;
  1417. end;
  1418. typesym : begin
  1419. if ptypesym(sym)^.definition^.deftype in [recorddef,objectdef] then
  1420. begin
  1421. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1422. instr.operands[operandnum].val:=0;
  1423. CreateVarInstr := TRUE;
  1424. Exit;
  1425. end;
  1426. end;
  1427. procsym : begin
  1428. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  1429. Message(assem_w_calling_overload_func);
  1430. { free the memory before changing the symbol name. }
  1431. if assigned(instr.operands[operandnum].ref.symbol) then
  1432. FreeMem(instr.operands[operandnum].ref.symbol,length(instr.operands[operandnum].ref.symbol^)+1);
  1433. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  1434. instr.operands[operandnum].size:=S_L;
  1435. instr.operands[operandnum].symbol:=newpasstr(pprocsym(sym)^.definition^.mangledname);
  1436. CreateVarInstr := TRUE;
  1437. Exit;
  1438. end;
  1439. else
  1440. begin
  1441. Message(assem_e_unsupported_symbol_type);
  1442. exit;
  1443. end;
  1444. end; {case}
  1445. end; { end looking for global variables .. }
  1446. end;
  1447. Function SearchLabel(const s: string; var hl: plabel): boolean;
  1448. {**********************************************************************}
  1449. { Description: Searches for a pascal label definition, first in the }
  1450. { local symbol list and then in the global symbol list. If found then }
  1451. { return pointer to label and return true, otherwise returns false. }
  1452. {**********************************************************************}
  1453. var
  1454. sym: psym;
  1455. Begin
  1456. SearchLabel := FALSE;
  1457. if assigned(aktprocsym) then
  1458. Begin
  1459. { Check the local constants }
  1460. if assigned(aktprocsym^.definition) then
  1461. Begin
  1462. if assigned(aktprocsym^.definition^.localst) then
  1463. sym := aktprocsym^.definition^.localst^.search(s)
  1464. else
  1465. sym := nil;
  1466. if assigned(sym) then
  1467. Begin
  1468. if (sym^.typ = labelsym) then
  1469. Begin
  1470. hl:=plabelsym(sym)^.number;
  1471. SearchLabel := TRUE;
  1472. exit;
  1473. end;
  1474. end;
  1475. end;
  1476. end;
  1477. { Check the global label symbols... }
  1478. getsym(s,false);
  1479. if srsym <> nil then
  1480. Begin
  1481. if (srsym^.typ=labelsym) then
  1482. Begin
  1483. hl:=plabelsym(srsym)^.number;
  1484. SearchLabel:= TRUE;
  1485. exit;
  1486. end;
  1487. end;
  1488. end;
  1489. {*************************************************************************}
  1490. { Instruction Generation Utilities }
  1491. {*************************************************************************}
  1492. Procedure ConcatString(p : paasmoutput;s:string);
  1493. {*********************************************************************}
  1494. { PROCEDURE ConcatString(s:string); }
  1495. { Description: This routine adds the character chain pointed to in }
  1496. { s to the instruction linked list. }
  1497. {*********************************************************************}
  1498. Var
  1499. pc: PChar;
  1500. Begin
  1501. getmem(pc,length(s)+1);
  1502. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1503. end;
  1504. Procedure ConcatPasString(p : paasmoutput;s:string);
  1505. {*********************************************************************}
  1506. { PROCEDURE ConcatPasString(s:string); }
  1507. { Description: This routine adds the character chain pointed to in }
  1508. { s to the instruction linked list, contrary to ConcatString it }
  1509. { uses a pascal style string, so it conserves null characters. }
  1510. {*********************************************************************}
  1511. Begin
  1512. p^.concat(new(pai_string,init(s)));
  1513. end;
  1514. Procedure ConcatDirect(p : paasmoutput;s:string);
  1515. {*********************************************************************}
  1516. { PROCEDURE ConcatDirect(s:string) }
  1517. { Description: This routine output the string directly to the asm }
  1518. { output, it is only sed when writing special labels in AT&T mode, }
  1519. { and should not be used without due consideration, since it may }
  1520. { cause problems. }
  1521. {*********************************************************************}
  1522. Var
  1523. pc: PChar;
  1524. Begin
  1525. getmem(pc,length(s)+1);
  1526. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1527. end;
  1528. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1529. {*********************************************************************}
  1530. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1531. { Description: This routine adds the value constant to the current }
  1532. { instruction linked list. }
  1533. { maxvalue -> indicates the size of the data to initialize: }
  1534. { $ff -> create a byte node. }
  1535. { $ffff -> create a word node. }
  1536. { $ffffffff -> create a dword node. }
  1537. {*********************************************************************}
  1538. Begin
  1539. if value > maxvalue then
  1540. Begin
  1541. Message(assem_e_constant_out_of_bounds);
  1542. { assuming a value of maxvalue }
  1543. value := maxvalue;
  1544. end;
  1545. if maxvalue = $ff then
  1546. p^.concat(new(pai_const,init_8bit(byte(value))))
  1547. else
  1548. if maxvalue = $ffff then
  1549. p^.concat(new(pai_const,init_16bit(word(value))))
  1550. else
  1551. if maxvalue = $ffffffff then
  1552. p^.concat(new(pai_const,init_32bit(longint(value))));
  1553. end;
  1554. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1555. {***********************************************************************}
  1556. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1557. { Description: This routine adds the value constant to the current }
  1558. { instruction linked list. }
  1559. { real_typ -> indicates the type of the real data to initialize: }
  1560. { s32real -> create a single node. }
  1561. { s64real -> create a double node. }
  1562. { s80real -> create an extended node. }
  1563. { s64bit -> create a comp node. }
  1564. { f32bit -> create a fixed node. (not used normally) }
  1565. {***********************************************************************}
  1566. Begin
  1567. case real_typ of
  1568. s32real : p^.concat(new(pai_single,init(value)));
  1569. s64real : p^.concat(new(pai_double,init(value)));
  1570. s80real : p^.concat(new(pai_extended,init(value)));
  1571. s64bit : p^.concat(new(pai_comp,init(value)));
  1572. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1573. end;
  1574. end;
  1575. Procedure ConcatLabel(p: paasmoutput;op : tasmop;var l : plabel);
  1576. {*********************************************************************}
  1577. { PROCEDURE ConcatLabel }
  1578. { Description: This routine either emits a label or a labeled }
  1579. { instruction to the linked list of instructions. }
  1580. {*********************************************************************}
  1581. begin
  1582. if op=A_LABEL then
  1583. p^.concat(new(pai_label,init(l)))
  1584. else
  1585. p^.concat(new(pai_labeled,init(op,l)))
  1586. end;
  1587. procedure ConcatPublic(p:paasmoutput;const s : string);
  1588. {*********************************************************************}
  1589. { PROCEDURE ConcatPublic }
  1590. { Description: This routine emits an global definition to the }
  1591. { linked list of instructions.(used by AT&T styled asm) }
  1592. {*********************************************************************}
  1593. begin
  1594. p^.concat(new(pai_symbol,init_global(s)));
  1595. { concat_internal(s,EXT_NEAR); done in aasm }
  1596. end;
  1597. procedure ConcatLocal(p:paasmoutput;const s : string);
  1598. {*********************************************************************}
  1599. { PROCEDURE ConcatLocal }
  1600. { Description: This routine emits an local definition to the }
  1601. { linked list of instructions. }
  1602. {*********************************************************************}
  1603. begin
  1604. p^.concat(new(pai_symbol,init(s)));
  1605. { concat_internal(s,EXT_NEAR); done in aasm }
  1606. end;
  1607. Procedure ConcatGlobalBss(const s : string;size : longint);
  1608. {*********************************************************************}
  1609. { PROCEDURE ConcatGlobalBss }
  1610. { Description: This routine emits an global datablock to the }
  1611. { linked list of instructions. }
  1612. {*********************************************************************}
  1613. begin
  1614. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1615. { concat_internal(s,EXT_NEAR); done in aasm }
  1616. end;
  1617. Procedure ConcatLocalBss(const s : string;size : longint);
  1618. {*********************************************************************}
  1619. { PROCEDURE ConcatLocalBss }
  1620. { Description: This routine emits a local datablcok to the }
  1621. { linked list of instructions. }
  1622. {*********************************************************************}
  1623. begin
  1624. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1625. { concat_internal(s,EXT_NEAR); done in aasm }
  1626. end;
  1627. { add to list of external labels }
  1628. Procedure ConcatExternal(const s : string;typ : texternal_typ);
  1629. {*********************************************************************}
  1630. { PROCEDURE ConcatExternal }
  1631. { Description: This routine emits an external definition to the }
  1632. { linked list of instructions.(used by AT&T styled asm) }
  1633. {*********************************************************************}
  1634. { check if in internal list and remove it there }
  1635. var p : pai_external;
  1636. begin
  1637. p:=search_assembler_symbol(internals,s,typ);
  1638. if p<>nil then internals^.remove(p);
  1639. concat_external(s,typ);
  1640. end;
  1641. { add to internal list of labels }
  1642. Procedure ConcatInternal(const s : string;typ : texternal_typ);
  1643. {*********************************************************************}
  1644. { PROCEDURE ConcatInternal }
  1645. { Description: This routine emits an internal definition of a symbol }
  1646. { (used by AT&T styled asm for undefined labels) }
  1647. {*********************************************************************}
  1648. begin
  1649. concat_internal(s,typ);
  1650. end;
  1651. end.
  1652. {
  1653. $Log$
  1654. Revision 1.1 1999-01-10 15:38:00 peter
  1655. * moved some tables from ra386*.pas -> i386.pas
  1656. + start of coff writer
  1657. * renamed asmutils unit to rautils
  1658. }