Rizin
unix-like reverse engineering framework and cli tools
assembler.h File Reference
#include "common.h"
#include "disassembler.h"

Go to the source code of this file.

Functions

RZ_IPI ut16 sh_assembler (RZ_NONNULL const char *buffer, ut64 pc, RZ_NULLABLE bool *success)
 Assemble instruction from SuperH-4 ISA FPU instructions not implemented yet. More...
 

Function Documentation

◆ sh_assembler()

RZ_IPI ut16 sh_assembler ( RZ_NONNULL const char *  buffer,
ut64  pc,
RZ_NULLABLE bool success 
)

Assemble instruction from SuperH-4 ISA FPU instructions not implemented yet.

Parameters
bufferInstruction string buffer
pcCurrent value of program counter
successStore bool whether the assembler succeeded or not (RZ_NULLABLE)
Returns
ut16 Opcode for the given instruction

Definition at line 402 of file assembler.c.

402  {
404  if (success) {
405  *success = true;
406  }
407 
408  char *mnem = NULL;
409  ut16 opcode = 0;
410  char *spaced = sh_op_space_params(buffer);
411  RzList *tokens = rz_str_split_duplist(spaced, " ", true);
412  free(spaced);
413  if (!tokens) {
414  goto bye;
415  }
416  RzListIter *itr, *tmp;
417  char *tok;
418  rz_list_foreach_safe (tokens, itr, tmp, tok) {
419  if (rz_str_is_whitespace(tok)) {
420  rz_list_delete(tokens, itr);
421  }
422  }
423  ut32 token_num = rz_list_length(tokens);
424  if (token_num == 0 || token_num > 3) {
425  RZ_LOG_ERROR("SuperH: Invalid number of operands in the instruction\n")
426  goto bye;
427  }
428 
429  mnem = (char *)rz_list_pop_head(tokens);
431  ut8 j = 0;
432  rz_list_foreach (tokens, itr, tok) {
433  sham[j] = sh_op_get_addr_mode(tok);
434  j++;
435  }
436 
437  for (ut16 i = 0; i < OPCODE_NUM; i++) {
438  if (!sh_op_compare(sh_op_lookup[i], mnem, sham)) {
439  continue;
440  }
441 
442  SHOpRaw raw = sh_op_lookup[i];
443  opcode = raw.opcode ^ raw.mask;
444  /* Now opcode only has the bits corresponding to the instruction
445  The bits corresponding to the operands are supposed to be calculated */
446 
447  // check for "weird" MOVL
448  if (raw.opcode == MOVL) {
449  char *reg_direct = rz_list_pop_head(tokens);
450  char *reg_disp_indirect = rz_list_pop_head(tokens);
451 
452  opcode |= sh_op_movl_param_bits(reg_direct, reg_disp_indirect);
453 
454  free(reg_direct);
455  free(reg_disp_indirect);
456  goto return_opcode;
457  }
458 
459  RzListIter *itr;
460  char *param;
461  j = 0;
462  rz_list_foreach (tokens, itr, param) {
463  opcode |= sh_op_param_bits(raw.param_builder[j], param, raw.scaling, pc);
464  j++;
465  }
466 
467  return_opcode:
468  rz_list_free(tokens);
469  free(mnem);
470  return opcode;
471  }
472 
473  RZ_LOG_ERROR("SuperH: Failed to assemble: \"%s\"\n", buffer);
474 
475 bye:
476  if (success) {
477  success = false;
478  }
479  rz_list_free(tokens);
480  free(mnem);
481  return 0;
482 }
#define mnem(n, mn)
lzma_index ** i
Definition: index.h:629
#define NULL
Definition: cris-opc.c:27
uint16_t ut16
uint32_t ut32
void bye(char *msg1, char *msg2)
Definition: gzappend.c:93
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
uint8_t ut8
Definition: lh5801.h:11
#define MOVL
Definition: common.h:70
RZ_API void rz_list_delete(RZ_NONNULL RzList *list, RZ_NONNULL RzListIter *iter)
Removes an entry in the list by using the RzListIter pointer.
Definition: list.c:162
RZ_API ut32 rz_list_length(RZ_NONNULL const RzList *list)
Returns the length of the list.
Definition: list.c:109
RZ_API RZ_OWN void * rz_list_pop_head(RZ_NONNULL RzList *list)
Removes and returns the first element of the list.
Definition: list.c:401
RZ_API void rz_list_free(RZ_NONNULL RzList *list)
Empties the list and frees the list pointer.
Definition: list.c:137
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
#define RZ_LOG_ERROR(fmtstr,...)
Definition: rz_log.h:58
RZ_API int RZ_API bool rz_str_is_whitespace(RZ_NONNULL const char *str)
Checks if the whole string is composed of whitespace.
Definition: str.c:2004
RZ_API RzList * rz_str_split_duplist(const char *str, const char *c, bool trim)
Split the string str according to the substring c and returns a RzList with the result.
Definition: str.c:3464
static bool sh_op_compare(SHOpRaw raw, const char *mnem, SHAddrHelper modes[])
Check whether raw and instruction to be formed using mnem and modes will be equivalent.
Definition: assembler.c:333
static char * sh_op_space_params(const char *buffer)
Replace all the commas outside operands with spaces (i.e. "space out" the operands)
Definition: assembler.c:26
static ut64 sh_op_movl_param_bits(const char *reg_direct, const char *reg_disp_indirect)
Special assembler functions for the operands of "weird" MOVL instruction.
Definition: assembler.c:209
static ut32 sh_op_param_bits(SHParamBuilder shb, const char *param, SHScaling scaling, ut64 pc)
Get the opcode bits corresponding to param, scaling, pc and addressing mode (shb.mode) This function ...
Definition: assembler.c:83
const SHOpRaw sh_op_lookup[]
Definition: lookup.c:7
static SHAddrHelper sh_op_get_addr_mode(const char *param)
Get the addressing mode being used in param.
Definition: assembler.c:251
const ut32 OPCODE_NUM
Definition: lookup.c:195
@ SH_REG_IND_SIZE
Definition: disassembler.h:149
@ SH_ADDR_INVALID
Definition: disassembler.h:18
Definition: buffer.h:15
SHScaling scaling
scaling for the opcode
Definition: common.h:29
SHParamBuilder param_builder[2]
param builders for the params
Definition: common.h:30
ut16 opcode
opcode
Definition: common.h:27
ut16 mask
mask for opcode to mask out param bits
Definition: common.h:28

References bye(), free(), i, sh_op_raw_t::mask, mnem, MOVL, NULL, sh_op_raw_t::opcode, OPCODE_NUM, sh_op_raw_t::param_builder, pc, rz_list_delete(), rz_list_free(), rz_list_length(), rz_list_pop_head(), RZ_LOG_ERROR, rz_return_val_if_fail, rz_str_is_whitespace(), rz_str_split_duplist(), sh_op_raw_t::scaling, SH_ADDR_INVALID, sh_op_compare(), sh_op_get_addr_mode(), sh_op_lookup, sh_op_movl_param_bits(), sh_op_param_bits(), sh_op_space_params(), SH_REG_IND_SIZE, and autogen_x86imm::tmp.

Referenced by assemble().