push
This commit is contained in:
140
srcs/srcs_asm/convert/conv_instru.c
Normal file
140
srcs/srcs_asm/convert/conv_instru.c
Normal file
@@ -0,0 +1,140 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* conv_instru.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/09 11:56:37 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 12:44:33 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_calc_byte(t_token *tk)
|
||||
{
|
||||
char *str_size;
|
||||
int size;
|
||||
char *complete;
|
||||
|
||||
str_size = ft_strnew(0);
|
||||
while (tk)
|
||||
{
|
||||
if (tk && ft_strcmp(tk->contents, ",") == 0)
|
||||
tk = tk->next;
|
||||
if (tk->type == DIRECT || tk->type == DIRECT_LABEL)
|
||||
str_size = ft_strextend(str_size, "10");
|
||||
else if (tk->type == INDIRECT || tk->type == INDIRECT_LABEL)
|
||||
str_size = ft_strextend(str_size, "11");
|
||||
else if (tk->type == REGISTER)
|
||||
str_size = ft_strextend(str_size, "01");
|
||||
tk = tk->next;
|
||||
}
|
||||
complete = ft_strnew_1(8 - ft_strlen(str_size));
|
||||
str_size = ft_strextend(str_size, complete);
|
||||
size = ft_atoi_bin(str_size);
|
||||
ft_strdel(&complete);
|
||||
ft_strdel(&str_size);
|
||||
return (size);
|
||||
}
|
||||
|
||||
unsigned char *ft_convert_lab(t_token *tk, int inst,
|
||||
int *size_param, t_pos_lab pos_lab)
|
||||
{
|
||||
unsigned char *param;
|
||||
|
||||
param = NULL;
|
||||
if (tk->type == DIRECT_LABEL)
|
||||
{
|
||||
*size_param = (g_tab[inst].size) ? 2 : 4;
|
||||
param = ft_conv_hexa(get_label(tk, pos_lab.lab)->place
|
||||
- pos_lab.pos, *size_param);
|
||||
}
|
||||
else if (tk->type == INDIRECT_LABEL)
|
||||
{
|
||||
*size_param = 2;
|
||||
param = ft_conv_hexa(get_label(tk, pos_lab.lab)->place
|
||||
- pos_lab.pos, *size_param);
|
||||
}
|
||||
return (param);
|
||||
}
|
||||
|
||||
unsigned char *ft_convert_one(t_token *tk, int inst,
|
||||
int *size_param, t_pos_lab pos_lab)
|
||||
{
|
||||
unsigned char *param;
|
||||
|
||||
*size_param = 0;
|
||||
param = NULL;
|
||||
if (tk->type == DIRECT)
|
||||
{
|
||||
*size_param = (g_tab[inst].size) ? 2 : 4;
|
||||
param = ft_conv_hexa(ft_atoi(tk->contents + 1), *size_param);
|
||||
}
|
||||
else if (tk->type == INDIRECT)
|
||||
{
|
||||
*size_param = 2;
|
||||
param = ft_conv_hexa(ft_atoi(tk->contents), *size_param);
|
||||
}
|
||||
else if (tk->type == REGISTER)
|
||||
{
|
||||
*size_param = 1;
|
||||
param = ft_conv_hexa(ft_atoi(tk->contents + 1), *size_param);
|
||||
}
|
||||
else
|
||||
param = ft_convert_lab(tk, inst, size_param, pos_lab);
|
||||
return (param);
|
||||
}
|
||||
|
||||
unsigned char *ft_str_params(t_token *tk, int inst,
|
||||
int *size_params, t_pos_lab pos_lab)
|
||||
{
|
||||
unsigned char *params;
|
||||
unsigned char *one;
|
||||
int size_one;
|
||||
|
||||
*size_params = 0;
|
||||
if (!(params = (unsigned char *)malloc(sizeof(*params))))
|
||||
return (NULL);
|
||||
while (tk)
|
||||
{
|
||||
if (tk && ft_strcmp(tk->contents, ",") == 0)
|
||||
tk = tk->next;
|
||||
else if (tk->type == COMMENT)
|
||||
break ;
|
||||
one = ft_convert_one(tk, inst, &size_one, pos_lab);
|
||||
params = ft_strextend_nm(params, one, *size_params, size_one);
|
||||
tk = tk->next;
|
||||
*size_params += size_one;
|
||||
}
|
||||
return (params);
|
||||
}
|
||||
|
||||
unsigned char *ft_conv_instru(t_token *tk, int *size_instru,
|
||||
t_pos_lab pos_lab)
|
||||
{
|
||||
int inst;
|
||||
unsigned char *instru;
|
||||
unsigned char *params;
|
||||
int size_params;
|
||||
|
||||
inst = is_inst(tk->contents);
|
||||
if (!(instru = (unsigned char *)malloc(sizeof(*instru)
|
||||
* (1 + g_tab[inst].byte))))
|
||||
return (NULL);
|
||||
instru[0] = g_tab[inst].op_code;
|
||||
params = ft_str_params(tk->next, inst, &size_params, pos_lab);
|
||||
if (g_tab[inst].byte)
|
||||
{
|
||||
instru[1] = ft_calc_byte(tk);
|
||||
instru = ft_strextend_nm(instru, params, 2, size_params);
|
||||
*size_instru = 2 + size_params;
|
||||
}
|
||||
else
|
||||
{
|
||||
instru = ft_strextend_nm(instru, params, 1, size_params);
|
||||
*size_instru = 1 + size_params;
|
||||
}
|
||||
return (instru);
|
||||
}
|
36
srcs/srcs_asm/convert/conv_utils.c
Normal file
36
srcs/srcs_asm/convert/conv_utils.c
Normal file
@@ -0,0 +1,36 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* conv_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/11 12:17:14 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/11 13:28:14 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
t_label *get_label(t_token *tk, t_label *label)
|
||||
{
|
||||
char *name;
|
||||
|
||||
if (tk->type == INDIRECT_LABEL)
|
||||
name = ft_strdup(tk->contents + 1);
|
||||
else if (tk->type == DIRECT_LABEL)
|
||||
name = ft_strdup(tk->contents + 2);
|
||||
else
|
||||
return (NULL);
|
||||
while (label)
|
||||
{
|
||||
if (ft_strequ(label->contents, name))
|
||||
{
|
||||
ft_strdel(&name);
|
||||
return (label);
|
||||
}
|
||||
label = label->next;
|
||||
}
|
||||
ft_strdel(&name);
|
||||
return (NULL);
|
||||
}
|
101
srcs/srcs_asm/convert/convert.c
Normal file
101
srcs/srcs_asm/convert/convert.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* convert.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/08 11:56:22 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 12:45:37 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_create_file(char **argv, unsigned char *buffer, int len)
|
||||
{
|
||||
char *str;
|
||||
int len_argv;
|
||||
int fd;
|
||||
int i;
|
||||
|
||||
len_argv = ft_strlen(argv[1]);
|
||||
if (!(str = malloc(sizeof(char) * len_argv - 1)))
|
||||
return (0);
|
||||
str[len_argv - 2] = 0;
|
||||
i = 0;
|
||||
while (argv[1][i] != '.')
|
||||
{
|
||||
str[i] = argv[1][i];
|
||||
i++;
|
||||
}
|
||||
str = ft_strextend(str, ".cor");
|
||||
ft_printf("Writing output program to %s\n", str);
|
||||
fd = open(str, O_CREAT, S_IRWXU, O_RDWR);
|
||||
close(fd);
|
||||
fd = open(str, O_RDWR);
|
||||
free(str);
|
||||
write(fd, buffer, len);
|
||||
close(fd);
|
||||
return (0);
|
||||
}
|
||||
|
||||
unsigned char *ft_convert_head(t_header *head)
|
||||
{
|
||||
unsigned char *buffer;
|
||||
unsigned char *conv_progsize;
|
||||
|
||||
if (!(buffer = (unsigned char *)ft_strnew(sizeof(*buffer) * 2192)))
|
||||
return (NULL);
|
||||
buffer[0] = 0;
|
||||
buffer[1] = 234;
|
||||
buffer[2] = 131;
|
||||
buffer[3] = 243;
|
||||
ft_strcpy_n(buffer, (unsigned char *)head->prog_name, 4, 128);
|
||||
conv_progsize = ft_conv_hexa(head->prog_size, 4);
|
||||
ft_strcpy_n(buffer, conv_progsize, 136, 4);
|
||||
ft_strcpy_n(buffer, (unsigned char *)head->comment, 140, 2048);
|
||||
ft_strdel((char **)&conv_progsize);
|
||||
return (buffer);
|
||||
}
|
||||
|
||||
void init_convert(unsigned char **buffer, t_header *head, int *pos)
|
||||
{
|
||||
unsigned char *conv_head;
|
||||
|
||||
*buffer = (unsigned char *)ft_strnew(2192 + head->prog_size);
|
||||
conv_head = ft_convert_head(head);
|
||||
*buffer = ft_strcpy_n(*buffer, conv_head, 0, 2192);
|
||||
*pos = 2192;
|
||||
ft_strdel((char **)&conv_head);
|
||||
}
|
||||
|
||||
void ft_convert(t_lst_tk *lst, t_header *head, char **argv,
|
||||
t_label *lab)
|
||||
{
|
||||
unsigned char *buffer;
|
||||
int pos;
|
||||
int size_instru;
|
||||
t_pos_lab pos_lab;
|
||||
|
||||
init_convert(&buffer, head, &pos);
|
||||
pos_lab.lab = lab;
|
||||
while (lst)
|
||||
{
|
||||
pos_lab.pos = pos - 2192;
|
||||
size_instru = 0;
|
||||
if (lst->token->type == INSTRUCTION)
|
||||
buffer = ft_strextend_nm(buffer, ft_conv_instru(lst->token,
|
||||
&size_instru, pos_lab), pos, size_instru);
|
||||
else if (lst->token->type == LABEL)
|
||||
{
|
||||
if (lst->token->next && lst->token->next->type != COMMENT)
|
||||
buffer = ft_strextend_nm(buffer, ft_conv_instru(
|
||||
lst->token->next, &size_instru, pos_lab), pos, size_instru);
|
||||
}
|
||||
lst = lst->next;
|
||||
pos += size_instru;
|
||||
}
|
||||
ft_create_file(argv, buffer, 2192 + head->prog_size);
|
||||
ft_strdel((char **)&buffer);
|
||||
}
|
Reference in New Issue
Block a user