AsmJsUtils.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. // Portions of this file are copyright 2014 Mozilla Foundation, available under the Apache 2.0 license.
  5. //-------------------------------------------------------------------------------------------------------
  6. //-------------------------------------------------------------------------------------------------------
  7. // Copyright 2014 Mozilla Foundation
  8. //
  9. // Licensed under the Apache License, Version 2.0 (the "License");
  10. // you may not use this file except in compliance with the License.
  11. // You may obtain a copy of the License at
  12. //
  13. // http ://www.apache.org/licenses/LICENSE-2.0
  14. //
  15. // Unless required by applicable law or agreed to in writing, software
  16. // distributed under the License is distributed on an "AS IS" BASIS,
  17. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. // See the License for the specific language governing permissions and
  19. // limitations under the License.
  20. //-------------------------------------------------------------------------------------------------------
  21. #pragma once
  22. #ifdef ASMJS_PLAT
  23. // Removed code from original location, if the expression is true, check if extra code needed
  24. #define MaybeTodo( expr ) AssertMsg( !(expr), "Unhandled scenario in asm.js" )
  25. namespace Js {
  26. Var AsmJsChangeHeapBuffer(RecyclableObject * function, CallInfo callInfo, ...);
  27. #pragma warning (suppress: 25057) // Suppress unannotated buffer warning
  28. void * UnboxAsmJsArguments(ScriptFunction* func, Var * origArgs, char * argDst, CallInfo callInfo);
  29. #if _M_X64
  30. int GetStackSizeForAsmJsUnboxing(ScriptFunction* func);
  31. Var BoxAsmJsReturnValue(ScriptFunction* func, int64 intRetVal, double doubleRetVal, float floatRetVal, __m128 simdReturn);
  32. #endif
  33. class AsmJsCompilationException
  34. {
  35. char16 msg_[256];
  36. public:
  37. AsmJsCompilationException( const char16* _msg, ... );
  38. inline char16* msg() { return msg_; }
  39. };
  40. class ParserWrapper
  41. {
  42. public:
  43. static PropertyName FunctionName( ParseNode *node );
  44. static PropertyName VariableName( ParseNode *node );
  45. static ParseNode* FunctionArgsList( ParseNode *node, ArgSlot &numformals );
  46. static ParseNode* NextVar( ParseNode *node );
  47. static ParseNode* NextInList( ParseNode *node );
  48. static inline ParseNode *GetListHead( ParseNode *node );
  49. static inline bool IsNameDeclaration(ParseNode *node);
  50. static inline bool IsUInt(ParseNode *node);
  51. static inline uint GetUInt(ParseNode *node);
  52. static inline bool IsNegativeZero(ParseNode* node);
  53. static inline bool IsMinInt(ParseNode *node){ return node && node->nop == knopFlt && node->sxFlt.maybeInt && node->sxFlt.dbl == -2147483648.0; };
  54. static inline bool IsUnsigned(ParseNode *node)
  55. {
  56. return node &&
  57. node->nop == knopFlt &&
  58. node->sxFlt.maybeInt &&
  59. node->sxFlt.dbl > (double)INT_MAX &&
  60. node->sxFlt.dbl <= (double)UINT_MAX;
  61. }
  62. static bool IsDefinition( ParseNode *arg );
  63. static bool ParseVarOrConstStatement( AsmJSParser &parser, ParseNode **var );
  64. static inline bool IsNumericLiteral(ParseNode* node) { return node && (node->nop == knopInt || node->nop == knopFlt); }
  65. static inline bool IsFroundNumericLiteral(ParseNode* node) { return node && (IsNumericLiteral(node) || IsNegativeZero(node)); }
  66. static inline ParseNode* GetUnaryNode( ParseNode* node ){Assert(IsNodeUnary(node));return node->sxUni.pnode1;}
  67. static inline ParseNode* GetBinaryLeft( ParseNode* node ){Assert(IsNodeBinary(node));return node->sxBin.pnode1;}
  68. static inline ParseNode* GetBinaryRight( ParseNode* node ){Assert(IsNodeBinary(node));return node->sxBin.pnode2;}
  69. static inline ParseNode* DotBase( ParseNode *node );
  70. static inline bool IsDotMember( ParseNode *node );
  71. static inline PropertyName DotMember( ParseNode *node );
  72. // Get the VarDecl from the node or nullptr if unable to find
  73. static ParseNode* GetVarDeclList(ParseNode* node);
  74. // Goes through the nodes until the end of the list of VarDecl
  75. static void ReachEndVarDeclList( ParseNode** node );
  76. // nop utils
  77. static inline bool IsNodeBinary (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & (fnopBin|fnopBinList)); }
  78. static inline bool IsNodeUnary (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopUni ); }
  79. static inline bool IsNodeConst (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopConst ); }
  80. static inline bool IsNodeLeaf (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopLeaf ); }
  81. static inline bool IsNodeRelational(ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopRel ); }
  82. static inline bool IsNodeAssignment(ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopAsg ); }
  83. static inline bool IsNodeBreak (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopBreak ); }
  84. static inline bool IsNodeContinue (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopContinue ); }
  85. static inline bool IsNodeCleanUp (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopCleanup ); }
  86. static inline bool IsNodeJump (ParseNode* pnode){ return pnode && !!(ParseNode::Grfnop(pnode->nop) & fnopJump ); }
  87. static inline bool IsNodeExpression(ParseNode* pnode){ return pnode && !(ParseNode::Grfnop(pnode->nop) & fnopNotExprStmt); }
  88. };
  89. bool ParserWrapper::IsNameDeclaration( ParseNode *node )
  90. {
  91. return node->nop == knopName || node->nop == knopStr;
  92. }
  93. bool ParserWrapper::IsNegativeZero(ParseNode *node)
  94. {
  95. return node && ((node->nop == knopFlt && JavascriptNumber::IsNegZero(node->sxFlt.dbl)) ||
  96. (node->nop == knopNeg && node->sxUni.pnode1->nop == knopInt && node->sxUni.pnode1->sxInt.lw == 0));
  97. }
  98. bool ParserWrapper::IsUInt( ParseNode *node )
  99. {
  100. return node->nop == knopInt || IsUnsigned(node);
  101. }
  102. uint ParserWrapper::GetUInt( ParseNode *node )
  103. {
  104. Assert( IsUInt( node ) );
  105. if( node->nop == knopInt )
  106. {
  107. return (uint)node->sxInt.lw;
  108. }
  109. Assert( node->nop == knopFlt );
  110. return (uint)node->sxFlt.dbl;
  111. }
  112. bool ParserWrapper::IsDotMember( ParseNode *node )
  113. {
  114. return node && (node->nop == knopDot || node->nop == knopIndex);
  115. }
  116. PropertyName ParserWrapper::DotMember( ParseNode *node )
  117. {
  118. Assert( IsDotMember(node) );
  119. if( IsNameDeclaration( GetBinaryRight( node ) ) )
  120. {
  121. return GetBinaryRight( node )->name();
  122. }
  123. return nullptr;
  124. }
  125. ParseNode* ParserWrapper::DotBase( ParseNode *node )
  126. {
  127. Assert( IsDotMember( node ) );
  128. return GetBinaryLeft( node );
  129. }
  130. ParseNode * ParserWrapper::GetListHead( ParseNode *node )
  131. {
  132. Assert( node->nop == knopList );
  133. return node->sxBin.pnode1;
  134. }
  135. };
  136. #endif