Skip to content

Commit

Permalink
Merge pull request VBrazhnik#2 from AnneMedvedeva/master
Browse files Browse the repository at this point in the history
asm was added
  • Loading branch information
VBrazhnik authored Dec 2, 2018
2 parents c524fe7 + fec56fa commit 1e1ab72
Show file tree
Hide file tree
Showing 26 changed files with 2,064 additions and 2 deletions.
28 changes: 26 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,31 @@ VS_SOURCES_LIST = \
VS_SOURCES = $(addprefix $(VS_SOURCES_DIRECTORY), $(VS_SOURCES_LIST))

ASM_SOURCES_DIRECTORY = ./sources/asm/
ASM_SOURCES_LIST =
ASM_SOURCES_LIST = \
label_op.c\
check_utils.c\
get_pos.c\
argument_check.c\
check_dir.c\
check_two_reg.c\
check_three_reg.c\
check_reg.c\
check_three_two_reg.c\
check_st.c\
check_sti.c\
check_aff.c\
check_arg.c\
get_arg.c\
read_bot_name_comm.c\
parse_operations.c\
read_label.c\
place_num.c\
main.c\
final_placing.c\
print_error.c\
ft_skip_whitespaces.c\
ft_strchri.c\
ft_strcmpi.c
ASM_SOURCES = $(addprefix $(ASM_SOURCES_DIRECTORY), $(ASM_SOURCES_LIST))

OBJECTS_DIRECTORY = ./objects/
Expand All @@ -92,7 +116,7 @@ RESET = \033[0m

.PHONY: all clean fclean re

all: $(COREWAR_NAME)
all: $(COREWAR_NAME) $(ASM_NAME)

$(COREWAR_NAME): $(FT_PRINTF) $(COREWAR_OBJECTS_DIRECTORY) $(VS_OBJECTS_DIRECTORY) $(COREWAR_OBJECTS) $(VS_OBJECTS)
@$(CC) $(FLAGS) $(COREWAR_LIBRARIES) $(COREWAR_INCLUDES) $(COREWAR_OBJECTS) $(VS_OBJECTS) -o $(COREWAR_NAME)
Expand Down
239 changes: 239 additions & 0 deletions includes/asm.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* asm.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amedvedi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2013/10/04 11:33:27 by amedvedi #+# #+# */
/* Updated: 2018/06/29 02:11:13 by amedvedi ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef ASM_H
# define ASM_H

# include "op.h"
# include "libft.h"
# include "get_next_line.h"
# include "ft_printf.h"
# include <stdio.h>

/*
** Bot name and command length
** 16: 4 - magic header, 4 - null, 4 - bot size, 4 - null
** 2192
*/

# define NAME_COMM_LENGTH (PROG_NAME_LENGTH + COMMENT_LENGTH + 16)

# define REG_CHAR 'r'

/*
** Label mention list
** idx ------position at the file
** size -----size of the label (4 or 2)
** value ----the given value to calculate the distance
*/

typedef struct s_replace
{
int idx;
int size;
int value;
struct s_replace *next;
} t_replace;

/*
** Labels list
** name ---------name of the label
** initialized ----indicator if the label was initialized
** pos ----------position at the file in the instruction section
** x ------------column position at file
** y ------------row position at file
** mention ------list of the mentions of the label from instructions
*/

typedef struct s_lbl
{
char *name;
int initialized;
int pos;
int x;
int y;
t_replace *mention;
struct s_lbl *next;
} t_lbl;

/*
** Main structure
** i -----------length of the whole given file
** pos ---------row position at file
** pos_x -------column position at file
** fd ----------file descriptor
** str ---------line from gnl
** r_str -------array used to save important parts from str
** file_name ----name for new .cor file
** data --------an array containing all written hex code
** label -------array containing a list of labels
*/

typedef struct s_val
{
int i;
int pos_y;
int pos_x;
int fd;
char *str;
char *r_str;
char *file_name;
char *data;
t_lbl *label;
} t_val;

/*
** Libft_functions
*/

int ft_skip_whitespaces(char *str, int i);
int ft_iswhitespace(int c);
int ft_strchri(const char *s, int c);
int ft_strcmpi(const char *s1, const char *s2);

/*
** Validating header
*/

void read_bot_name_comm(t_val *assm, int i, int ret);
void check_comm(t_val *assm, int i);

/*
** Validating instruction name and parsing
*/

void read_bot_op(t_val *assm);

/*
** All instruction parsing
** check_dir - live, zjump, fork, lfork
** check_two_reg - ld, lld
** check_three_reg - and, or, xor
** check_reg - sub, add
** check_three_two_reg - ldi, lldi
** in own files - st, sti, aff
*/

void check_live(t_val *assm, int i);
void check_zjmp(t_val *assm, int i);
void check_fork(t_val *assm, int i);
void check_lfork(t_val *assm, int i);

/*
** -------------------------------------------check_two_reg
*/

void check_ld(t_val *assm, int i);
void check_lld(t_val *assm, int i);

/*
** -------------------------------------------check_three_reg
*/

void check_and(t_val *assm, int i);
void check_or(t_val *assm, int i);
void check_xor(t_val *assm, int i);

/*
** -------------------------------------------check_reg
*/

void check_sub(t_val *assm, int i);
void check_add(t_val *assm, int i);

/*
** -------------------------------------------check_three_two_reg
*/

void check_ldi(t_val *assm, int i);
void check_lldi(t_val *assm, int i);

/*
** -------------------------------------------own files
*/

void check_st(t_val *assm, int i);
void check_sti(t_val *assm, int i);
void check_aff(t_val *assm, int i);

/*
** Validating arguments in instruction
*/

int get_type(char c, int *codage);
int check_all_arg(t_val *assm, int i, int pos, int *codage);
int check_reg_dir(t_val *assm, int i, int pos, int *codage);
int check_dir_ind(t_val *assm, int i, int pos, int *codage);

/*
** Argument validation
*/

void args_amount(t_val *assm, char *str, int nbr, int w);
int reg_arg(t_val *assm, char *str, int i, int w);
int dir_arg(t_val *assm, char *str, int i, int w);
int ind_arg(t_val *assm, char *str, int i, int w);
int skip_comma(t_val *assm, int i, char *error, int w);

/*
** Getting argument information
*/

int get_arg(t_val *assm, int i, int size, int octet);
int get_size(int dir_size, int type, int mode);

/*
** Minimized parsing of first and second argument
*/

int get_arg_first_2(t_val *assm, int i, int codage,
int i_pos);
int get_arg_second_2(t_val *assm, int i, int codage,
int i_pos);
int get_arg_first_4(t_val *assm, int i, int codage,
int i_pos);
int get_arg_second_4(t_val *assm, int i, int codage,
int i_pos);

/*
** Label operations
*/

int read_label(t_val *assm, int init, int pc, int size);
void create_label(t_val *assm, t_lbl **tmp, int pc,
int size);
void upd_label(t_val *assm, t_lbl **tmp, int pc, int size);
void place_num(t_val *assm, int start, int i);

/*
** Error
*/

void print_error(t_val *assm, char *str);
void local_error(t_val *assm, char *msg, int what);
void print_error_label(t_val *assm, char *msg, int x, int y);

/*
** Free
*/

void free_asm(t_val *assm);

/*
** Final preparations
*/

void place_labels(t_val *assm);
void place_size(t_val *assm);
void create_file(t_val *assm);

#endif
52 changes: 52 additions & 0 deletions sources/asm/argument_check.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* argument_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amedvedi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/01 15:16:56 by amedvedi #+# #+# */
/* Updated: 2018/12/01 15:16:58 by amedvedi ### ########.fr */
/* */
/* ************************************************************************** */

#include "asm.h"

int check_all_arg(t_val *assm, int i, int pos, int *codage)
{
int ret;

if ((ret = get_type(assm->r_str[i], codage)) == 1)
i = reg_arg(assm, assm->r_str, i, pos);
else if (ret == 2)
i = dir_arg(assm, assm->r_str, i, pos);
else
i = ind_arg(assm, assm->r_str, i, pos);
return (i);
}

int check_reg_dir(t_val *assm, int i, int pos, int *codage)
{
int ret;

if ((ret = get_type(assm->r_str[i], codage)) == 1)
i = reg_arg(assm, assm->r_str, i, pos);
else if (ret == 2)
i = dir_arg(assm, assm->r_str, i, pos);
else
local_error(assm, "Bad argument\n", pos + 1);
return (i);
}

int check_dir_ind(t_val *assm, int i, int pos, int *codage)
{
int ret;

if ((ret = get_type(assm->r_str[i], codage)) == 2)
i = dir_arg(assm, assm->r_str, i, pos);
else if (ret == 3)
i = ind_arg(assm, assm->r_str, i, pos);
else
local_error(assm, "Bad argument\n", pos + 1);
return (i);
}
40 changes: 40 additions & 0 deletions sources/asm/check_aff.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_aff.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: amedvedi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/12/01 15:17:04 by amedvedi #+# #+# */
/* Updated: 2018/12/01 15:17:06 by amedvedi ### ########.fr */
/* */
/* ************************************************************************** */

#include "asm.h"

/*
** AFF
*/

void args_aff(t_val *assm, int i)
{
int tmp;

tmp = i;
if (!(assm->r_str = ft_strsub(assm->str, i, ft_strlen(assm->str) - i)))
print_error(assm, "Malloc error (counting args)\n");
args_amount(assm, assm->r_str, 1, i);
i = reg_arg(assm, assm->r_str, 0, tmp);
if (assm->r_str[i])
local_error(assm, "Bad character after arguments\n", tmp + 1);
}

void check_aff(t_val *assm, int i)
{
ft_strdel(&assm->r_str);
assm->data[assm->i++] = 16;
i = ft_skip_whitespaces(assm->str, i);
args_aff(assm, i);
assm->data[assm->i++] = 0x40;
get_arg(assm, 1, 1, 2);
}
Loading

0 comments on commit 1e1ab72

Please sign in to comment.