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);
|
||||
}
|
65
srcs/srcs_asm/lexer/ft_automaton.c
Normal file
65
srcs/srcs_asm/lexer/ft_automaton.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_automaton.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/06 14:48:26 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 14:30:23 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_cal_i_string(int i_matrice, char *line, int *nb_char_token)
|
||||
{
|
||||
int i_string;
|
||||
|
||||
i_string = 0;
|
||||
if (line[*nb_char_token] == 0
|
||||
|| (i_matrice == 21 && line[*nb_char_token] != '\n'))
|
||||
i_string = 16;
|
||||
else
|
||||
while (!(ft_line_strchr(g_string_automaton[i_string],
|
||||
line[*nb_char_token])) && i_string < 15)
|
||||
i_string++;
|
||||
return (i_string);
|
||||
}
|
||||
|
||||
static int ft_end_error(int *nb_char_token, char *line)
|
||||
{
|
||||
g_nb_char = g_nb_char + *nb_char_token;
|
||||
ft_printf("error: syntax %d:%d char: %c\n",
|
||||
g_nb_line, g_nb_char + 1, line[*nb_char_token]);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_automaton(char *line, t_token *token, t_label *label,
|
||||
int *nb_char_token)
|
||||
{
|
||||
int i_matrice;
|
||||
int i_string;
|
||||
int type;
|
||||
|
||||
i_matrice = 0;
|
||||
while (1)
|
||||
{
|
||||
type = i_matrice;
|
||||
i_string = ft_cal_i_string(i_matrice, line, nb_char_token);
|
||||
i_matrice = g_matrice_automaton[i_matrice][i_string];
|
||||
if (i_matrice == -2 || i_matrice == -3)
|
||||
{
|
||||
if (i_matrice == -3)
|
||||
*nb_char_token = *nb_char_token - 1;
|
||||
g_nb_char = g_nb_char + *nb_char_token;
|
||||
ft_create_token_label(ft_create_string(line, *nb_char_token),
|
||||
type, token, label);
|
||||
return (1);
|
||||
}
|
||||
else if (i_matrice == -1)
|
||||
return (ft_end_error(nb_char_token, line));
|
||||
else
|
||||
*nb_char_token = *nb_char_token + 1;
|
||||
}
|
||||
}
|
95
srcs/srcs_asm/lexer/ft_lexer.c
Normal file
95
srcs/srcs_asm/lexer/ft_lexer.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_lexer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/30 10:48:23 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 16:20:57 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_free_line(char **line, char **tmp)
|
||||
{
|
||||
free(*line);
|
||||
free(*tmp);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void ft_free_error(t_token *token, t_label *label,
|
||||
t_token *list_token)
|
||||
{
|
||||
t_token *tmp;
|
||||
|
||||
free(token);
|
||||
free(label);
|
||||
if (list_token != NULL)
|
||||
{
|
||||
while (list_token != NULL)
|
||||
{
|
||||
tmp = list_token->next;
|
||||
free(list_token->contents);
|
||||
free(list_token);
|
||||
list_token = tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
t_token *ft_lexer_token(char **line, t_label **list_label)
|
||||
{
|
||||
t_token *token;
|
||||
t_label *label;
|
||||
t_token *list_token;
|
||||
int nb_char_token;
|
||||
|
||||
list_token = NULL;
|
||||
while ((*line)[0] != '\n' && (*line)[0] != 0)
|
||||
{
|
||||
if (!(token = malloc(sizeof(t_token))))
|
||||
return (NULL);
|
||||
if (!(label = malloc(sizeof(t_label))))
|
||||
return (NULL);
|
||||
nb_char_token = 0;
|
||||
if (!(ft_automaton(*line, token, label, &nb_char_token)))
|
||||
{
|
||||
ft_free_error(token, label, list_token);
|
||||
return (NULL);
|
||||
}
|
||||
ft_append_token_label(token, &list_token, label, list_label);
|
||||
if (!(line = ft_cut_line(line, nb_char_token)))
|
||||
break ;
|
||||
}
|
||||
return (list_token);
|
||||
}
|
||||
|
||||
int ft_lexer(t_lst_tk **list, t_label **list_label, char **argv)
|
||||
{
|
||||
char *line;
|
||||
int fd;
|
||||
static char *tmp;
|
||||
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
line = NULL;
|
||||
while (get_next_line(fd, &line, &tmp) == 1)
|
||||
{
|
||||
g_nb_char = 0;
|
||||
g_nb_line++;
|
||||
if (!(line = ft_check_line(line, fd)))
|
||||
return (ft_free_line(&line, &tmp));
|
||||
if (!(ft_empty_line(line)))
|
||||
{
|
||||
free(line);
|
||||
continue ;
|
||||
}
|
||||
if (!(ft_append_lst_tk(ft_lexer_token(&line, list_label), list)))
|
||||
return (ft_free_line(&line, &tmp));
|
||||
free(line);
|
||||
}
|
||||
if (line != NULL)
|
||||
free(line);
|
||||
free(tmp);
|
||||
return (1);
|
||||
}
|
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c
Normal file
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_lst_tk.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_append_lst_tk.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/24 11:38:38 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/01 11:22:10 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
t_lst_tk *ft_create_lst_tk(t_token *token)
|
||||
{
|
||||
t_lst_tk *new;
|
||||
|
||||
if (token == NULL)
|
||||
return (NULL);
|
||||
if (!(new = (t_lst_tk*)malloc(sizeof(t_lst_tk))))
|
||||
return (NULL);
|
||||
new->token = token;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
}
|
||||
|
||||
int ft_append_lst_tk(t_token *token, t_lst_tk **list)
|
||||
{
|
||||
t_lst_tk *begin;
|
||||
|
||||
begin = *list;
|
||||
if (token == NULL)
|
||||
return (0);
|
||||
if (*list == NULL)
|
||||
{
|
||||
if (!(*list = ft_create_lst_tk(token)))
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
while ((*list)->next != NULL)
|
||||
*list = (*list)->next;
|
||||
if (!((*list)->next = ft_create_lst_tk(token)))
|
||||
return (1);
|
||||
*list = begin;
|
||||
}
|
||||
return (1);
|
||||
}
|
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c
Normal file
49
srcs/srcs_asm/lexer/lib_lexer/ft_append_token_label.c
Normal file
@@ -0,0 +1,49 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_append_token_label.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:42:16 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/15 12:56:53 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static void ft_append_label(t_label *label, t_label **list_label)
|
||||
{
|
||||
t_label *tmp_label;
|
||||
|
||||
if (*list_label == NULL)
|
||||
*list_label = label;
|
||||
else
|
||||
{
|
||||
tmp_label = *list_label;
|
||||
while (tmp_label->next != NULL)
|
||||
tmp_label = tmp_label->next;
|
||||
tmp_label->next = label;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_append_token_label(t_token *token, t_token **list_token,
|
||||
t_label *label, t_label **list_label)
|
||||
{
|
||||
t_token *tmp_token;
|
||||
|
||||
if (*list_token == NULL)
|
||||
*list_token = token;
|
||||
else
|
||||
{
|
||||
tmp_token = *list_token;
|
||||
while (tmp_token->next != NULL)
|
||||
tmp_token = tmp_token->next;
|
||||
tmp_token->next = token;
|
||||
tmp_token->line = g_nb_line;
|
||||
}
|
||||
if (token->type == 1)
|
||||
ft_append_label(label, list_label);
|
||||
else
|
||||
free(label);
|
||||
}
|
64
srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c
Normal file
64
srcs/srcs_asm/lexer/lib_lexer/ft_check_line.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_check_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/24 11:24:43 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/15 18:19:05 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_cal_state(char *line, int state)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
state = 0;
|
||||
while (line[i] != 0)
|
||||
{
|
||||
if (line[i] == '"')
|
||||
state = state + 1;
|
||||
i++;
|
||||
}
|
||||
return (state);
|
||||
}
|
||||
|
||||
static char *ft_error_gnl(char *str, char *line, char *tmp)
|
||||
{
|
||||
ft_strdel(&str);
|
||||
ft_strdel(&line);
|
||||
ft_strdel(&tmp);
|
||||
ft_printf("error: quote `\n");
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
char *ft_check_line(char *line, int fd)
|
||||
{
|
||||
char *str;
|
||||
static char *tmp;
|
||||
int i;
|
||||
int state;
|
||||
|
||||
i = 0;
|
||||
state = 0;
|
||||
state = ft_cal_state(line, state);
|
||||
while (state % 2 == 1)
|
||||
{
|
||||
if (get_next_line(fd, &str, &tmp) != 1)
|
||||
return (ft_error_gnl(str, line, tmp));
|
||||
if (!(line = ft_strextend(line, "\n")))
|
||||
return (NULL);
|
||||
if (!(line = ft_strextend(line, str)))
|
||||
return (NULL);
|
||||
ft_strdel(&str);
|
||||
g_nb_line++;
|
||||
g_nb_char = 0;
|
||||
state = ft_cal_state(line, state);
|
||||
}
|
||||
line = ft_strextend(line, "\n");
|
||||
return (line);
|
||||
}
|
57
srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c
Normal file
57
srcs/srcs_asm/lexer/lib_lexer/ft_create_string.c
Normal file
@@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_create_string.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:57:44 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/09 10:44:33 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static char *ft_cut_string(char *str)
|
||||
{
|
||||
char *str2;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = ft_strlen(str);
|
||||
j = 0;
|
||||
while (str[i - 1] == ' ' || str[i - 1] == '\t'
|
||||
|| str[i - 1] == '\v' || str[i - 1] == '\n')
|
||||
i--;
|
||||
if (!(str2 = ft_strnew(i + 1)))
|
||||
return (NULL);
|
||||
while (j < i)
|
||||
{
|
||||
str2[j] = str[j];
|
||||
j++;
|
||||
}
|
||||
free(str);
|
||||
str = str2;
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_create_string(char *line, int nb_char_token)
|
||||
{
|
||||
char *str;
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (line[i] == ' ' || line[i] == '\t' || line[i] == '\v')
|
||||
i++;
|
||||
if (!(str = ft_strnew(nb_char_token - i)))
|
||||
return (NULL);
|
||||
while (j <= nb_char_token - i - 1)
|
||||
{
|
||||
str[j] = line[j + i];
|
||||
j++;
|
||||
}
|
||||
str = ft_cut_string(str);
|
||||
return (str);
|
||||
}
|
27
srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c
Normal file
27
srcs/srcs_asm/lexer/lib_lexer/ft_create_token_label.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_create_token_label.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 14:03:27 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/15 11:03:20 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
void ft_create_token_label(char *str, int type, t_token *token,
|
||||
t_label *label)
|
||||
{
|
||||
if (type == 1)
|
||||
{
|
||||
label->contents = str;
|
||||
label->next = NULL;
|
||||
}
|
||||
token->type = type;
|
||||
token->line = g_nb_line;
|
||||
token->contents = str;
|
||||
token->next = NULL;
|
||||
}
|
54
srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c
Normal file
54
srcs/srcs_asm/lexer/lib_lexer/ft_cut_line.c
Normal file
@@ -0,0 +1,54 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_cut_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:53:27 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/09 10:44:50 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static int ft_check_line2(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != 0)
|
||||
{
|
||||
if (str[i] != ' ' && str[i] != '\n' && str[i] != '\t' && str[i] != '\v')
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char **ft_cut_line(char **line, int nb_char_token)
|
||||
{
|
||||
int len;
|
||||
int i;
|
||||
char *str;
|
||||
|
||||
i = 0;
|
||||
len = ft_strlen(*line);
|
||||
if (len < 1 || len == nb_char_token)
|
||||
return (NULL);
|
||||
if (!(str = ft_strnew(len - nb_char_token + 1)))
|
||||
return (NULL);
|
||||
while ((*line)[i + nb_char_token] != 0)
|
||||
{
|
||||
str[i] = (*line)[nb_char_token + i];
|
||||
i++;
|
||||
}
|
||||
if (!(ft_check_line2(str)))
|
||||
{
|
||||
free(str);
|
||||
return (NULL);
|
||||
}
|
||||
free(*line);
|
||||
*line = str;
|
||||
return (line);
|
||||
}
|
26
srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c
Normal file
26
srcs/srcs_asm/lexer/lib_lexer/ft_empty_line.c
Normal file
@@ -0,0 +1,26 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_empty_line.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/24 15:13:26 by jfleury #+# #+# */
|
||||
/* Updated: 2019/06/24 15:16:17 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_empty_line(char *line)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (line[i] == ' ' || line[i] == '\t' || line[i] == '\v'
|
||||
|| line[i] == '\n')
|
||||
i++;
|
||||
if (line[i] == 0)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
62
srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c
Normal file
62
srcs/srcs_asm/lexer/lib_lexer/ft_free_list.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_free_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/25 11:04:41 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/13 15:11:14 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static void ft_free_token(t_token *token)
|
||||
{
|
||||
t_token *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (token != NULL)
|
||||
{
|
||||
tmp = token->next;
|
||||
ft_strdel(&token->contents);
|
||||
free(token);
|
||||
token = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
static void ft_free_label(t_label *label)
|
||||
{
|
||||
t_label *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (label != NULL)
|
||||
{
|
||||
tmp = label->next;
|
||||
free(label);
|
||||
label = tmp;
|
||||
}
|
||||
}
|
||||
|
||||
void ft_free_list(t_lst_tk *list, t_label *label)
|
||||
{
|
||||
t_lst_tk *tmp;
|
||||
|
||||
tmp = NULL;
|
||||
while (list != NULL)
|
||||
{
|
||||
tmp = list->next;
|
||||
ft_free_token(list->token);
|
||||
free(list);
|
||||
list = tmp;
|
||||
}
|
||||
ft_free_label(label);
|
||||
}
|
||||
|
||||
void ft_free_all(t_lst_tk *list, t_label *label, t_header *head)
|
||||
{
|
||||
if (head)
|
||||
free(head);
|
||||
ft_free_list(list, label);
|
||||
}
|
27
srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c
Normal file
27
srcs/srcs_asm/lexer/lib_lexer/ft_line_strchr.c
Normal file
@@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_line_strchr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/14 13:55:57 by jfleury #+# #+# */
|
||||
/* Updated: 2019/06/14 13:56:11 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_line_strchr(char *str, char c)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i] != 0)
|
||||
{
|
||||
if (str[i] == c)
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
73
srcs/srcs_asm/lexer/src_variable/var_automaton.c
Normal file
73
srcs/srcs_asm/lexer/src_variable/var_automaton.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* var_automaton.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/06 10:13:09 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/02 17:22:36 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int g_nb_line = 0;
|
||||
|
||||
int g_nb_char = 0;
|
||||
|
||||
int g_nb_token = 0;
|
||||
|
||||
int g_matrice_automaton[30][17] =
|
||||
{
|
||||
{23, 23, -1, 24, 26, 17, 19, 9, 29, -1, 12, 21, 21, 22, 0, 0, -1},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{-3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3},
|
||||
{-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2},
|
||||
{12, 12, -1, 12, -1, 1, -1, -1, -1, -1, 12, -1, -1, -1, -1, 1, -1},
|
||||
{13, 13, -1, 13, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 2, 2, -1},
|
||||
{-1, -1, -1, 14, -1, -1, -1, 3, -1, -1, -1, 3, 3, -1, 3, 3, -1},
|
||||
{15, 15, -1, 15, -1, -1, -1, 4, -1, -1, 15, 4, 4, -1, 4, 4, -1},
|
||||
{-1, -1, -1, 16, -1, -1, -1, 5, -1, -1, -1, 5, 5, -1, 5, 5, -1},
|
||||
{17, 17, -1, 17, -1, -1, -1, 6, -1, -1, 17, 6, 6, -1, 6, 6, -1},
|
||||
{-1, -1, -1, 18, -1, -1, -1, 7, -1, -1, -1, 7, 7, -1, 7, 7, -1},
|
||||
{19, 19, 19, 19, 19, 19, 8, 19, 19, 19, 19, 19, 19, 19, 19, 19, 19},
|
||||
{-1, -1, -1, -1, -1, -1, -1, 9, -1, -1, -1, -1, -1, -1, 9, 9, -1},
|
||||
{21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 10, 21},
|
||||
{22, 22, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 11, 11, -1},
|
||||
{23, 23, -1, 25, -1, 1, -1, -1, -1, -1, 12, -1, -1, -1, 2, 2, -1},
|
||||
{12, 12, -1, 24, -1, 1, -1, 5, -1, -1, 12, -1, -1, -1, 5, 5, -1},
|
||||
{12, 12, -1, 25, -1, 1, -1, 7, -1, -1, 12, 7, 7, -1, 7, 7, -1},
|
||||
{-1, -1, -1, 14, -1, 15, -1, -1, 14, -1, -1, -1, -1, -1, -1, -1, -1},
|
||||
{-1, -1, -1, 16, -1, 17, -1, -1, 16, -1, -1, -1, -1, -1, -1, -1, -1},
|
||||
{-1, -1, -1, 28, -1, -1, -1, 6, -1, -1, -1, 6, 6, -1, 6, 6, -1},
|
||||
{-1, -1, -1, 16, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
|
||||
};
|
||||
|
||||
char *g_string_automaton[16] =
|
||||
{
|
||||
"r",
|
||||
"abcdefghijklmnopqstuvwxyz",
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
||||
"0123456789",
|
||||
"%",
|
||||
":",
|
||||
"\"",
|
||||
",",
|
||||
"-",
|
||||
"+",
|
||||
"_",
|
||||
"#",
|
||||
";",
|
||||
".",
|
||||
"\t\f ",
|
||||
"\n"
|
||||
};
|
112
srcs/srcs_asm/main_asm.c
Normal file
112
srcs/srcs_asm/main_asm.c
Normal file
@@ -0,0 +1,112 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* main_asm.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/28 17:08:18 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 16:23:43 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static void ft_check_fd(int fd, char **argv)
|
||||
{
|
||||
if (fd == -1)
|
||||
{
|
||||
ft_printf("error: no such file or directory: '%s'\n", argv[1]);
|
||||
close(fd);
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
static void ft_check_arg(int argc, char **argv)
|
||||
{
|
||||
int i;
|
||||
int fd;
|
||||
|
||||
if (argc == 1)
|
||||
{
|
||||
ft_printf("usage: ./asm <file.s>\n");
|
||||
exit(0);
|
||||
}
|
||||
if (argc > 2)
|
||||
{
|
||||
ft_printf("error: too many files / "
|
||||
"only one file accepted\n");
|
||||
exit(0);
|
||||
}
|
||||
i = ft_strlen(argv[1]);
|
||||
fd = open(argv[1], O_RDONLY);
|
||||
ft_check_fd(fd, argv);
|
||||
close(fd);
|
||||
if (i >= 3 && argv[1][i - 1] == 's' && argv[1][i - 2] == '.')
|
||||
return ;
|
||||
else
|
||||
{
|
||||
ft_printf("error: invalid file\n");
|
||||
exit(0);
|
||||
}
|
||||
}
|
||||
|
||||
t_header *init_header(void)
|
||||
{
|
||||
t_header *head;
|
||||
int i;
|
||||
|
||||
if (!(head = (t_header *)ft_memalloc(sizeof(t_header))))
|
||||
return (NULL);
|
||||
head->magic = 15369203;
|
||||
head->prog_size = 0;
|
||||
i = -1;
|
||||
while (++i < PROG_NAME_LENGTH)
|
||||
head->prog_name[i] = 0;
|
||||
head->prog_name[i] = '\0';
|
||||
i = -1;
|
||||
while (++i < COMMENT_LENGTH)
|
||||
head->comment[i] = 0;
|
||||
head->comment[i] = '\0';
|
||||
return (head);
|
||||
}
|
||||
|
||||
static int ft_lexer_parser(t_header *head, t_label *list_label,
|
||||
t_lst_tk *lst_tk, char **argv)
|
||||
{
|
||||
if (!(ft_lexer(&lst_tk, &list_label, argv)))
|
||||
{
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
return (0);
|
||||
}
|
||||
if (lst_tk == NULL)
|
||||
{
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
ft_printf("error: empty file / directory file\n");
|
||||
return (0);
|
||||
}
|
||||
if (!(ft_parser(lst_tk, list_label, head)))
|
||||
{
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
return (0);
|
||||
}
|
||||
ft_convert(lst_tk, head, argv, list_label);
|
||||
ft_free_all(lst_tk, list_label, head);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
t_header *head;
|
||||
t_label *list_label;
|
||||
t_lst_tk *lst_tk;
|
||||
|
||||
ft_check_arg(argc, argv);
|
||||
if (!(head = init_header()))
|
||||
return (0);
|
||||
list_label = NULL;
|
||||
lst_tk = NULL;
|
||||
if (!(ft_lexer_parser(head, list_label, lst_tk, argv)))
|
||||
return (0);
|
||||
return (0);
|
||||
}
|
39
srcs/srcs_asm/op.c
Normal file
39
srcs/srcs_asm/op.c
Normal file
@@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* op.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jfleury <jfleury@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/13 12:13:23 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/15 18:30:09 by jfleury ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
t_op g_tab[17] =
|
||||
{
|
||||
{"live", 1, {T_DIR}, 1, 10, "alive", 0, 0},
|
||||
{"ld", 2, {T_DIR | T_IND, T_REG}, 2, 5, "load", 1, 0},
|
||||
{"st", 2, {T_REG, T_IND | T_REG}, 3, 5, "store", 1, 0},
|
||||
{"add", 3, {T_REG, T_REG, T_REG}, 4, 10, "addition", 1, 0},
|
||||
{"sub", 3, {T_REG, T_REG, T_REG}, 5, 10, "soustraction", 1, 0},
|
||||
{"and", 3, {T_REG | T_DIR | T_IND, T_REG | T_IND | T_DIR, T_REG}, 6, 6,
|
||||
"et (and r1, r2, r3 r1&r2 -> r3", 1, 0},
|
||||
{"or", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 7, 6,
|
||||
"ou (or r1, r2, r3 r1 | r2 -> r3", 1, 0},
|
||||
{"xor", 3, {T_REG | T_IND | T_DIR, T_REG | T_IND | T_DIR, T_REG}, 8, 6,
|
||||
"ou (xor r1, r2, r3 r1^r2 -> r3", 1, 0},
|
||||
{"zjmp", 1, {T_DIR | T_IND}, 9, 20, "jump if zero", 0, 1},
|
||||
{"ldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG | T_IND, T_REG}, 10, 25,
|
||||
"load index", 1, 1},
|
||||
{"sti", 3, {T_REG, T_REG | T_DIR | T_IND, T_DIR | T_REG}, 11, 25,
|
||||
"store index", 1, 1},
|
||||
{"fork", 1, {T_DIR | T_IND}, 12, 800, "fork", 0, 1},
|
||||
{"lld", 2, {T_DIR | T_IND, T_REG}, 13, 10, "long load", 1, 0},
|
||||
{"lldi", 3, {T_REG | T_DIR | T_IND, T_DIR | T_REG, T_REG}, 14, 50,
|
||||
"long load index", 1, 1},
|
||||
{"lfork", 1, {T_DIR}, 15, 1000, "long fork", 0, 1},
|
||||
{"aff", 1, {T_REG}, 16, 2, "aff", 1, 0}
|
||||
};
|
83
srcs/srcs_asm/parser/ft_cal_progsize.c
Normal file
83
srcs/srcs_asm/parser/ft_cal_progsize.c
Normal file
@@ -0,0 +1,83 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_cal_progsize.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/07/02 15:20:20 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/13 15:26:28 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
static unsigned char *ft_resize(unsigned char *str, int size)
|
||||
{
|
||||
int len;
|
||||
int i;
|
||||
int j;
|
||||
unsigned char *str2;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
len = ft_strlen((char*)str);
|
||||
if (!(str2 = malloc(sizeof(char) * (size * 2))))
|
||||
return (NULL);
|
||||
while (i < size * 2 - len)
|
||||
{
|
||||
str2[i] = '0';
|
||||
i++;
|
||||
}
|
||||
while (i < size * 2)
|
||||
{
|
||||
str2[i] = str[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
free(str);
|
||||
return (str2);
|
||||
}
|
||||
|
||||
static unsigned char ft_atoi_hex(unsigned char *str)
|
||||
{
|
||||
unsigned char nb;
|
||||
|
||||
if (str[0] >= '0' && str[0] <= '9')
|
||||
str[0] = str[0] - 48;
|
||||
if (str[1] >= '0' && str[1] <= '9')
|
||||
str[1] = str[1] - 48;
|
||||
if (str[0] >= 'a' && str[0] <= 'f')
|
||||
str[0] = str[0] - 87;
|
||||
if (str[1] >= 'a' && str[1] <= 'f')
|
||||
str[1] = str[1] - 87;
|
||||
if (str[0] != 0)
|
||||
nb = (str[0] * 16) + str[1];
|
||||
else
|
||||
nb = str[1];
|
||||
return (nb);
|
||||
}
|
||||
|
||||
unsigned char *ft_conv_hexa(int nbr, int size)
|
||||
{
|
||||
unsigned char *str;
|
||||
unsigned char *final_str;
|
||||
int len;
|
||||
|
||||
if (!(final_str = (unsigned char*)ft_strnew(size)))
|
||||
return (NULL);
|
||||
if (size == 2)
|
||||
str = (unsigned char *)ft_itoa_base_short(nbr, 16);
|
||||
else
|
||||
str = (unsigned char *)ft_itoa_base_int(nbr, 16);
|
||||
len = ft_strlen((char*)str);
|
||||
if (len < size * 2)
|
||||
str = ft_resize(str, size);
|
||||
while (size * 2 > 0)
|
||||
{
|
||||
final_str[size - 1] = ft_atoi_hex(str + ((size * 2) - 2));
|
||||
size--;
|
||||
}
|
||||
ft_strdel((char **)&str);
|
||||
return (final_str);
|
||||
}
|
77
srcs/srcs_asm/parser/ft_parse_command.c
Normal file
77
srcs/srcs_asm/parser/ft_parse_command.c
Normal file
@@ -0,0 +1,77 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parse_command.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/17 11:24:46 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 15:35:23 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int ft_parse_name(t_token *tk, t_header *head)
|
||||
{
|
||||
if (!tk || !tk->contents)
|
||||
return (ft_printf("error: no champion name in the file\n") && 0);
|
||||
if (ft_strlen(tk->contents) > PROG_NAME_LENGTH + 2)
|
||||
{
|
||||
return (ft_printf("error: Champion name too long (Max length 128)\n")
|
||||
&& 0);
|
||||
}
|
||||
if (tk->next && tk->next->type != COMMENT)
|
||||
{
|
||||
return (ft_printf("error: syntax: Line %d: "
|
||||
"too mush arguments for champion name\n", tk->line) && 0);
|
||||
}
|
||||
ft_strcpy_1(head->prog_name, tk->contents);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_parse_champ_cmt(t_token *tk, t_header *head)
|
||||
{
|
||||
if (!tk || !tk->contents)
|
||||
return (ft_printf("error: no champion comment in the file\n") && 0);
|
||||
if (ft_strlen(tk->contents) > COMMENT_LENGTH + 2)
|
||||
{
|
||||
return (ft_printf("error: Champion comment too long "
|
||||
"(Max length 2048)\n")
|
||||
&& 0);
|
||||
}
|
||||
if (tk->next && tk->next->type != COMMENT)
|
||||
{
|
||||
return (ft_printf("error: syntax: Line %d: "
|
||||
"too mush arguments for champion comment\n", tk->line) && 0);
|
||||
}
|
||||
ft_strcpy_1(head->comment, tk->contents);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int parse_header(t_lst_tk **lst, t_header *head)
|
||||
{
|
||||
while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT)
|
||||
(*lst) = (*lst)->next;
|
||||
if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND
|
||||
&& ft_strcmp((*lst)->token->contents, NAME_CMD_STRING) == 0)
|
||||
{
|
||||
if (!ft_parse_name((*lst)->token->next, head))
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (ft_printf("error: no name in the file\n") && 0);
|
||||
(*lst) = (*lst)->next;
|
||||
while ((*lst) && (*lst)->token && (*lst)->token->type == COMMENT)
|
||||
(*lst) = (*lst)->next;
|
||||
if ((*lst) && (*lst)->token && (*lst)->token->type == COMMAND
|
||||
&& ft_strcmp((*lst)->token->contents, COMMENT_CMD_STRING) == 0)
|
||||
{
|
||||
if (!ft_parse_champ_cmt((*lst)->token->next, head))
|
||||
return (0);
|
||||
}
|
||||
else
|
||||
return (ft_printf("error: no champion comment in the file\n") && 0);
|
||||
(*lst) = (*lst)->next;
|
||||
return (1);
|
||||
}
|
125
srcs/srcs_asm/parser/ft_parse_instruc.c
Normal file
125
srcs/srcs_asm/parser/ft_parse_instruc.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parse_instruc.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/17 16:17:56 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/16 15:36:42 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
int is_inst(char *name)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!name)
|
||||
return (-1);
|
||||
i = 0;
|
||||
while (i < 16 && ft_strcmp(name, g_tab[i].name) != 0)
|
||||
i++;
|
||||
if (i == 16)
|
||||
return (-1);
|
||||
else
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_verif_param(t_token *tk, int inst, int num_param, t_label *label)
|
||||
{
|
||||
int type;
|
||||
|
||||
if (tk->type == INDIRECT
|
||||
|| (tk->type == INDIRECT_LABEL && get_label(tk, label)))
|
||||
type = T_IND;
|
||||
else if (tk->type == DIRECT
|
||||
|| (tk->type == DIRECT_LABEL && get_label(tk, label)))
|
||||
type = T_DIR;
|
||||
else if (tk->type == REGISTER && ft_atoi(tk->contents + 1) < 100)
|
||||
type = T_REG;
|
||||
else
|
||||
type = 0;
|
||||
if (g_tab[inst].poss[num_param] & type)
|
||||
return (1);
|
||||
else
|
||||
{
|
||||
ft_printf("error: l%d : Invalid parameter %d for instruction %s\n",
|
||||
tk->line, num_param, g_tab[inst].name);
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
|
||||
int ft_correct_instru(t_token *tk, int i, t_label *label, int line)
|
||||
{
|
||||
int num_param;
|
||||
|
||||
num_param = 0;
|
||||
while (tk && num_param < g_tab[i].nb_param)
|
||||
{
|
||||
if (ft_verif_param(tk, i, num_param, label))
|
||||
{
|
||||
tk = tk->next;
|
||||
if (!tk && num_param == g_tab[i].nb_param - 1)
|
||||
return (1);
|
||||
else if (tk && tk->type == COMMENT
|
||||
&& !tk->next && num_param == g_tab[i].nb_param - 1)
|
||||
return (1);
|
||||
else if (tk && ft_strcmp(tk->contents, ",") == 0)
|
||||
num_param++;
|
||||
else
|
||||
return (ft_printf("error: l%d: bad param %d for instru %s\n",
|
||||
line, num_param + 1, g_tab[i].name) && 0);
|
||||
}
|
||||
else
|
||||
return (0);
|
||||
tk = tk->next;
|
||||
}
|
||||
return (ft_printf("error: l%d: bad params for instru %s\n",
|
||||
line, g_tab[i].name) && 0);
|
||||
}
|
||||
|
||||
int ft_calc_size(t_token *tk, int inst)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = 1 + g_tab[inst].byte;
|
||||
while (tk)
|
||||
{
|
||||
if (tk->type == INDIRECT || tk->type == INDIRECT_LABEL)
|
||||
size += 2;
|
||||
else if (tk->type == DIRECT || tk->type == DIRECT_LABEL)
|
||||
size += (g_tab[inst].size) ? 2 : 4;
|
||||
else if (tk->type == REGISTER)
|
||||
size += 1;
|
||||
tk = tk->next;
|
||||
}
|
||||
return (size);
|
||||
}
|
||||
|
||||
int ft_parse_instruct(t_token *tk, t_label *label, unsigned int *size)
|
||||
{
|
||||
int inst;
|
||||
|
||||
inst = is_inst(tk->contents);
|
||||
if (inst < 0)
|
||||
{
|
||||
return (ft_printf("error: l%d: invalid instruction '%s'\n",
|
||||
tk->line, tk->contents) && 0);
|
||||
}
|
||||
if (!tk->next)
|
||||
{
|
||||
return (ft_printf("error: l%d: no params for instru %s\n",
|
||||
tk->line, tk->contents) && 0);
|
||||
}
|
||||
if (ft_correct_instru(tk->next, inst, label, tk->line))
|
||||
{
|
||||
*size += ft_calc_size(tk->next, inst);
|
||||
return (1);
|
||||
}
|
||||
else
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
}
|
73
srcs/srcs_asm/parser/ft_parser.c
Normal file
73
srcs/srcs_asm/parser/ft_parser.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/05/30 10:48:19 by jfleury #+# #+# */
|
||||
/* Updated: 2019/07/16 12:23:55 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
void ft_label_pos(t_label *label, char *tk_lab, int size)
|
||||
{
|
||||
while (label)
|
||||
{
|
||||
if (ft_strequ(label->contents, tk_lab))
|
||||
{
|
||||
label->place = size;
|
||||
}
|
||||
label = label->next;
|
||||
}
|
||||
}
|
||||
|
||||
void init_label(t_label *label)
|
||||
{
|
||||
while (label)
|
||||
{
|
||||
label->contents[ft_strlen(label->contents) - 1] = '\0';
|
||||
label = label->next;
|
||||
}
|
||||
}
|
||||
|
||||
int ft_parse_label(t_label *label, t_token *token, unsigned int *size)
|
||||
{
|
||||
ft_label_pos(label, token->contents, *size);
|
||||
if (!token->next || token->next->type == COMMENT)
|
||||
return (1);
|
||||
else if (!ft_parse_instruct(token->next, label, size))
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_parser(t_lst_tk *lst, t_label *label, t_header *head)
|
||||
{
|
||||
unsigned int size;
|
||||
|
||||
init_label(label);
|
||||
size = 0;
|
||||
if (!parse_header(&lst, head))
|
||||
return (0);
|
||||
while (lst)
|
||||
{
|
||||
if (lst->token->type == INSTRUCTION)
|
||||
{
|
||||
if (!ft_parse_instruct(lst->token, label, &size))
|
||||
return (0);
|
||||
}
|
||||
else if (lst->token->type == LABEL)
|
||||
{
|
||||
if (!(ft_parse_label(label, lst->token, &size)))
|
||||
return (0);
|
||||
}
|
||||
else if (lst->token->type != COMMENT)
|
||||
return (0);
|
||||
lst = lst->next;
|
||||
}
|
||||
if ((head->prog_size = size) == 0)
|
||||
return (ft_printf("error: no instruction in the file\n") && 0);
|
||||
return (1);
|
||||
}
|
95
srcs/srcs_asm/parser/parse_utils.c
Normal file
95
srcs/srcs_asm/parser/parse_utils.c
Normal file
@@ -0,0 +1,95 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parse_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mdchane <mdchane@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/06/06 17:08:25 by mdchane #+# #+# */
|
||||
/* Updated: 2019/07/13 17:14:11 by mdchane ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "asm.h"
|
||||
|
||||
unsigned char *ft_strextend_nm(unsigned char *s1,
|
||||
char unsigned *s2, int n, int m)
|
||||
{
|
||||
unsigned char *str;
|
||||
int i;
|
||||
|
||||
if (s1 == NULL || s2 == NULL)
|
||||
return (NULL);
|
||||
i = n + m;
|
||||
if (!(str = (unsigned char *)malloc(sizeof(*str) * i + 1)))
|
||||
return (NULL);
|
||||
ft_strcpy_n(str, s1, 0, n);
|
||||
ft_strcpy_n(str, s2, n, m);
|
||||
ft_strdel((char **)&s1);
|
||||
ft_strdel((char **)&s2);
|
||||
return (str);
|
||||
}
|
||||
|
||||
int ft_tklen(t_token *tk)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (!tk)
|
||||
return (0);
|
||||
while (tk)
|
||||
{
|
||||
i++;
|
||||
tk = tk->next;
|
||||
}
|
||||
return (i);
|
||||
}
|
||||
|
||||
unsigned char *ft_strcpy_n(unsigned char *dest,
|
||||
const unsigned char *src, int n, int m)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
while (i < n)
|
||||
i++;
|
||||
j = 0;
|
||||
while (j < m)
|
||||
{
|
||||
dest[i] = src[j];
|
||||
i++;
|
||||
j++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
||||
|
||||
char *ft_strnew_1(size_t size)
|
||||
{
|
||||
char *str;
|
||||
|
||||
if (!(str = (char*)malloc(sizeof(char) * size + 1)))
|
||||
return (NULL);
|
||||
ft_memset(str, '0', size);
|
||||
str[size] = '\0';
|
||||
return (str);
|
||||
}
|
||||
|
||||
char *ft_strcpy_1(char *dest, const char *src)
|
||||
{
|
||||
unsigned int i;
|
||||
unsigned int j;
|
||||
|
||||
i = 0;
|
||||
j = 0;
|
||||
while (src[j] != '\0')
|
||||
{
|
||||
if (src[j] != '"')
|
||||
{
|
||||
dest[i] = src[j];
|
||||
i++;
|
||||
}
|
||||
j++;
|
||||
}
|
||||
return (dest);
|
||||
}
|
Reference in New Issue
Block a user