⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 eval.c

📁 一个很有名的硬件模拟器。可以模拟CPU
💻 C
📖 第 1 页 / 共 3 页
字号:
      return (md_addr_t)val.value.as_double;    case et_float:      return (md_addr_t)val.value.as_float;#ifdef HOST_HAS_QWORD    case et_qword:      return (md_addr_t)val.value.as_qword;    case et_sqword:      return (md_addr_t)val.value.as_sqword;#endif /* HOST_HAS_QWORD */    case et_addr:      return val.value.as_addr;    case et_uint:      return (md_addr_t)val.value.as_uint;    case et_int:      return (md_addr_t)val.value.as_int;    case et_symbol:      panic("symbol used in expression");    default:      panic("illegal arithmetic expression conversion");    }}/* eval_value_t (any numeric type) -> unsigned int */unsigned inteval_as_uint(struct eval_value_t val){  switch (val.type)    {    case et_double:      return (unsigned int)val.value.as_double;    case et_float:      return (unsigned int)val.value.as_float;#ifdef HOST_HAS_QWORD    case et_qword:      return (unsigned int)val.value.as_qword;    case et_sqword:      return (unsigned int)val.value.as_sqword;#endif /* HOST_HAS_QWORD */    case et_addr:      return (unsigned int)val.value.as_addr;    case et_uint:      return val.value.as_uint;    case et_int:      return (unsigned int)val.value.as_int;    case et_symbol:      panic("symbol used in expression");    default:      panic("illegal arithmetic expression conversion");    }}/* eval_value_t (any numeric type) -> int */inteval_as_int(struct eval_value_t val){  switch (val.type)    {    case et_double:      return (int)val.value.as_double;    case et_float:      return (int)val.value.as_float;#ifdef HOST_HAS_QWORD    case et_qword:      return (int)val.value.as_qword;    case et_sqword:      return (int)val.value.as_sqword;#endif /* HOST_HAS_QWORD */    case et_addr:      return (int)val.value.as_addr;    case et_uint:      return (int)val.value.as_uint;    case et_int:      return val.value.as_int;    case et_symbol:      panic("symbol used in expression");    default:      panic("illegal arithmetic expression conversion");    }}/* * arithmetic intrinsics operations, used during expression evaluation *//* compute <val1> + <val2> */static struct eval_value_tf_add(struct eval_value_t val1, struct eval_value_t val2){  enum eval_type_t et;  struct eval_value_t val;  /* symbols are not allowed in arithmetic expressions */  if (val1.type == et_symbol || val2.type == et_symbol)    {      eval_error = ERR_BADEXPR;      return err_value;    }  /* get result type, and perform operation in that type */  et = result_type(val1.type, val2.type);  switch (et)    {    case et_double:      val.type = et_double;      val.value.as_double = eval_as_double(val1) + eval_as_double(val2);      break;    case et_float:      val.type = et_float;      val.value.as_float = eval_as_float(val1) + eval_as_float(val2);      break;#ifdef HOST_HAS_QWORD    case et_qword:      val.type = et_qword;      val.value.as_qword = eval_as_qword(val1) + eval_as_qword(val2);      break;    case et_sqword:      val.type = et_sqword;      val.value.as_sqword = eval_as_sqword(val1) + eval_as_sqword(val2);      break;#endif /* HOST_HAS_QWORD */    case et_addr:      val.type = et_addr;      val.value.as_addr = eval_as_addr(val1) + eval_as_addr(val2);      break;    case et_uint:      val.type = et_uint;      val.value.as_uint = eval_as_uint(val1) + eval_as_uint(val2);      break;    case et_int:      val.type = et_int;      val.value.as_int = eval_as_int(val1) + eval_as_int(val2);      break;    default:      panic("bogus expression type");    }  return val;}/* compute <val1> - <val2> */static struct eval_value_tf_sub(struct eval_value_t val1, struct eval_value_t val2){  enum eval_type_t et;  struct eval_value_t val;  /* symbols are not allowed in arithmetic expressions */  if (val1.type == et_symbol || val2.type == et_symbol)    {      eval_error = ERR_BADEXPR;      return err_value;    }  /* get result type, and perform operation in that type */  et = result_type(val1.type, val2.type);  switch (et)    {    case et_double:      val.type = et_double;      val.value.as_double = eval_as_double(val1) - eval_as_double(val2);      break;    case et_float:      val.type = et_float;      val.value.as_float = eval_as_float(val1) - eval_as_float(val2);      break;#ifdef HOST_HAS_QWORD    case et_qword:      val.type = et_qword;      val.value.as_qword = eval_as_qword(val1) - eval_as_qword(val2);      break;    case et_sqword:      val.type = et_sqword;      val.value.as_sqword = eval_as_sqword(val1) - eval_as_sqword(val2);      break;#endif /* HOST_HAS_QWORD */    case et_addr:      val.type = et_addr;      val.value.as_addr = eval_as_addr(val1) - eval_as_addr(val2);      break;    case et_uint:      val.type = et_uint;      val.value.as_uint = eval_as_uint(val1) - eval_as_uint(val2);      break;    case et_int:      val.type = et_int;      val.value.as_int = eval_as_int(val1) - eval_as_int(val2);      break;    default:      panic("bogus expression type");    }  return val;}/* compute <val1> * <val2> */static struct eval_value_tf_mult(struct eval_value_t val1, struct eval_value_t val2){  enum eval_type_t et;  struct eval_value_t val;  /* symbols are not allowed in arithmetic expressions */  if (val1.type == et_symbol || val2.type == et_symbol)    {      eval_error = ERR_BADEXPR;      return err_value;    }  /* get result type, and perform operation in that type */  et = result_type(val1.type, val2.type);  switch (et)    {    case et_double:      val.type = et_double;      val.value.as_double = eval_as_double(val1) * eval_as_double(val2);      break;    case et_float:      val.type = et_float;      val.value.as_float = eval_as_float(val1) * eval_as_float(val2);      break;#ifdef HOST_HAS_QWORD    case et_qword:      val.type = et_qword;      val.value.as_qword = eval_as_qword(val1) * eval_as_qword(val2);      break;    case et_sqword:      val.type = et_sqword;      val.value.as_sqword = eval_as_sqword(val1) * eval_as_sqword(val2);      break;#endif /* HOST_HAS_QWORD */    case et_addr:      val.type = et_addr;      val.value.as_addr = eval_as_addr(val1) * eval_as_addr(val2);      break;    case et_uint:      val.type = et_uint;      val.value.as_uint = eval_as_uint(val1) * eval_as_uint(val2);      break;    case et_int:      val.type = et_int;      val.value.as_int = eval_as_int(val1) * eval_as_int(val2);      break;    default:      panic("bogus expression type");    }  return val;}/* compute <val1> / <val2> */static struct eval_value_tf_div(struct eval_value_t val1, struct eval_value_t val2){  enum eval_type_t et;  struct eval_value_t val;  /* symbols are not allowed in arithmetic expressions */  if (val1.type == et_symbol || val2.type == et_symbol)    {      eval_error = ERR_BADEXPR;      return err_value;    }  /* get result type, and perform operation in that type */  et = result_type(val1.type, val2.type);  switch (et)    {    case et_double:      val.type = et_double;      val.value.as_double = eval_as_double(val1) / eval_as_double(val2);      break;    case et_float:      val.type = et_float;      val.value.as_float = eval_as_float(val1) / eval_as_float(val2);      break;#ifdef HOST_HAS_QWORD    case et_qword:      val.type = et_qword;      val.value.as_qword = eval_as_qword(val1) / eval_as_qword(val2);      break;    case et_sqword:      val.type = et_sqword;      val.value.as_sqword = eval_as_sqword(val1) / eval_as_sqword(val2);      break;#endif /* HOST_HAS_QWORD */    case et_addr:      val.type = et_addr;      val.value.as_addr = eval_as_addr(val1) / eval_as_addr(val2);      break;    case et_uint:      val.type = et_uint;      val.value.as_uint = eval_as_uint(val1) / eval_as_uint(val2);      break;    case et_int:      val.type = et_int;      val.value.as_int = eval_as_int(val1) / eval_as_int(val2);      break;    default:      panic("bogus expression type");    }  return val;}/* compute - <val1> */static struct eval_value_tf_neg(struct eval_value_t val1){  struct eval_value_t val;  /* symbols are not allowed in arithmetic expressions */  if (val1.type == et_symbol)    {      eval_error = ERR_BADEXPR;      return err_value;    }  /* result type is the same as the operand type */  switch (val1.type)    {    case et_double:      val.type = et_double;      val.value.as_double = - val1.value.as_double;      break;    case et_float:      val.type = et_float;      val.value.as_float = - val1.value.as_float;      break;#ifdef HOST_HAS_QWORD    case et_qword:      val.type = et_sqword;      val.value.as_qword = - (sqword_t)val1.value.as_qword;      break;    case et_sqword:      val.type = et_sqword;      val.value.as_sqword = - val1.value.as_sqword;      break;#endif /* HOST_HAS_QWORD */    case et_addr:      val.type = et_addr;      val.value.as_addr = - val1.value.as_addr;      break;    case et_uint:      if ((unsigned int)val1.value.as_uint > 2147483648U)	{	  /* promote type */#ifdef HOST_HAS_QWORD	  val.type = et_sqword;	  val.value.as_sqword = - ((sqword_t)val1.value.as_uint);#else /* !HOST_HAS_QWORD */	  val.type = et_double;	  val.value.as_double = - ((double)val1.value.as_uint);#endif /* HOST_HAS_QWORD */	}      else	{	  /* don't promote type */	  val.type = et_int;	  val.value.as_int = - ((int)val1.value.as_uint);	}      break;    case et_int:      if ((unsigned int)val1.value.as_int == 0x80000000U)	{	  /* promote type */	  val.type = et_uint;	  val.value.as_uint = 2147483648U;	}      else	{	  /* don't promote type */	  val.type = et_int;	  val.value.as_int = - val1.value.as_int;	}      break;    default:      panic("bogus expression type");    }  return val;}/* compute val1 == 0 */static intf_eq_zero(struct eval_value_t val1){  int val;  /* symbols are not allowed in arithmetic expressions */  if (val1.type == et_symbol)    {      eval_error = ERR_BADEXPR;      return FALSE;    }  switch (val1.type)    {    case et_double:      val = val1.value.as_double == 0.0;      break;    case et_float:      val = val1.value.as_float == 0.0;      break;#ifdef HOST_HAS_QWORD    case et_qword:      val = val1.value.as_qword == 0;      break;    case et_sqword:      val = val1.value.as_sqword == 0;      break;#endif /* HOST_HAS_QWORD */    case et_addr:      val = val1.value.as_addr == 0;      break;    case et_uint:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -