Blame pttc/include/yasm.h

Packit b1f7ae
/*
Packit b1f7ae
 * Copyright (c) 2013-2017, Intel Corporation
Packit b1f7ae
 *
Packit b1f7ae
 * Redistribution and use in source and binary forms, with or without
Packit b1f7ae
 * modification, are permitted provided that the following conditions are met:
Packit b1f7ae
 *
Packit b1f7ae
 *  * Redistributions of source code must retain the above copyright notice,
Packit b1f7ae
 *    this list of conditions and the following disclaimer.
Packit b1f7ae
 *  * Redistributions in binary form must reproduce the above copyright notice,
Packit b1f7ae
 *    this list of conditions and the following disclaimer in the documentation
Packit b1f7ae
 *    and/or other materials provided with the distribution.
Packit b1f7ae
 *  * Neither the name of Intel Corporation nor the names of its contributors
Packit b1f7ae
 *    may be used to endorse or promote products derived from this software
Packit b1f7ae
 *    without specific prior written permission.
Packit b1f7ae
 *
Packit b1f7ae
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit b1f7ae
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit b1f7ae
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit b1f7ae
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit b1f7ae
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit b1f7ae
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit b1f7ae
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit b1f7ae
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit b1f7ae
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit b1f7ae
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit b1f7ae
 * POSSIBILITY OF SUCH DAMAGE.
Packit b1f7ae
 */
Packit b1f7ae
Packit b1f7ae
#ifndef YASM_H
Packit b1f7ae
#define YASM_H
Packit b1f7ae
Packit b1f7ae
#include "file.h"
Packit b1f7ae
#include "util.h"
Packit b1f7ae
Packit b1f7ae
#include <stdint.h>
Packit b1f7ae
Packit b1f7ae
/* Parses all labels in @t and appends them to @l.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 * Returns -err_section if @t contains a "[section]" yasm directive.
Packit b1f7ae
 * Sections are currently not supported.
Packit b1f7ae
 * Returns -err_label_addr if the address for a label could not be
Packit b1f7ae
 * determined.
Packit b1f7ae
 */
Packit b1f7ae
extern int parse_yasm_labels(struct label *l, const struct text *t);
Packit b1f7ae
Packit b1f7ae
/* Modifies @s, so it can be used as a label, if @s actually looks like
Packit b1f7ae
 * a label.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns true if @s looks like a label; false otherwise.
Packit b1f7ae
 * Returns -err_internal if @l or @name is the NULL pointer.
Packit b1f7ae
 */
Packit b1f7ae
extern int make_label(char *s);
Packit b1f7ae
Packit b1f7ae
/* Represents the state of the pt directive parser.  The parser uses the
Packit b1f7ae
 * canonical yasm lst file syntax to follow all asm source files that
Packit b1f7ae
 * were used during a yasm run.  The lst file stores information about
Packit b1f7ae
 * these files in terms of line numbers and line increments.  With this
Packit b1f7ae
 * information the contents of the lst file can be correlated to the
Packit b1f7ae
 * actual source files.
Packit b1f7ae
 */
Packit b1f7ae
struct state {
Packit b1f7ae
	/* Current line number.  */
Packit b1f7ae
	int n;
Packit b1f7ae
Packit b1f7ae
	/* Current line increment for this file.  */
Packit b1f7ae
	int inc;
Packit b1f7ae
Packit b1f7ae
	/* Current filename.  */
Packit b1f7ae
	char *filename;
Packit b1f7ae
Packit b1f7ae
	/* Pointer to the current line.  */
Packit b1f7ae
	char *line;
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
/* Allocates new state.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns a non-NULL state object on success; NULL otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern struct state *st_alloc(void);
Packit b1f7ae
Packit b1f7ae
/* Deallocates and clears all fields of @st.
Packit b1f7ae
 * If @st is the NULL pointer, nothing happens.
Packit b1f7ae
 */
Packit b1f7ae
extern void st_free(struct state *st);
Packit b1f7ae
Packit b1f7ae
/* Prints @s to stderr enriched with @st's file and line information.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns @errcode on success.
Packit b1f7ae
 * Returns -err_internal if @st is the NULL pointer or @errcode is
Packit b1f7ae
 * not negative.
Packit b1f7ae
 */
Packit b1f7ae
extern int st_print_err(const struct state *st, const char *s, int errcode);
Packit b1f7ae
Packit b1f7ae
/* Represents a pt directive with name and payload.  */
Packit b1f7ae
struct pt_directive {
Packit b1f7ae
	/* Name of the directive.  */
Packit b1f7ae
	char *name;
Packit b1f7ae
Packit b1f7ae
	/* Length of name.  */
Packit b1f7ae
	size_t nlen;
Packit b1f7ae
Packit b1f7ae
	/* Everything between the '(' and ')' in the directive.  */
Packit b1f7ae
	char *payload;
Packit b1f7ae
Packit b1f7ae
	/* Length of payoad.  */
Packit b1f7ae
	size_t plen;
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
/* Allocates a new pt directive that can hold a directive name and
Packit b1f7ae
 * payload of no more than @n characters.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns a non-NULL pt directive object on success; NULL otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern struct pt_directive *pd_alloc(size_t n);
Packit b1f7ae
Packit b1f7ae
/* Deallocates and clears all fields of @pd.
Packit b1f7ae
 * If @pd is the NULL pointer, nothing happens.
Packit b1f7ae
 */
Packit b1f7ae
extern void pd_free(struct pt_directive *pd);
Packit b1f7ae
Packit b1f7ae
/* Copies @name and @payload to the corresponding fields in @pd.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 * Returns -err_internal if @pd or @name or @payload is the NULL
Packit b1f7ae
 * pointer.
Packit b1f7ae
 */
Packit b1f7ae
extern int pd_set(struct pt_directive *pd, const char *name,
Packit b1f7ae
		   const char *payload);
Packit b1f7ae
Packit b1f7ae
/* Parses a pt directive from @st and stores it in @pd.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 * Returns -err_internal if @pd or @st is the NULL pointer.
Packit b1f7ae
 */
Packit b1f7ae
extern int pd_parse(struct pt_directive *pd, struct state *st);
Packit b1f7ae
Packit b1f7ae
/* Represents a yasm assembled file.  */
Packit b1f7ae
struct yasm {
Packit b1f7ae
	/* Filename of the .asm file.  */
Packit b1f7ae
	char *pttfile;
Packit b1f7ae
Packit b1f7ae
	/* Filename of the .lst file.  It is the concatenation of
Packit b1f7ae
	 * fileroot and ".lst".
Packit b1f7ae
	 */
Packit b1f7ae
	char *lstfile;
Packit b1f7ae
Packit b1f7ae
	/* Filename of the .bin file.  It is the concatenation of
Packit b1f7ae
	 * fileroot and ".bin".
Packit b1f7ae
	 */
Packit b1f7ae
	char *binfile;
Packit b1f7ae
Packit b1f7ae
	/* Fileroot is the pttfile filename, but with a trailing file
Packit b1f7ae
	 * extension removed.  It is used to create files based on the
Packit b1f7ae
	 * pttfile and is also used to create the .pt and .exp files
Packit b1f7ae
	 * during the parsing step.
Packit b1f7ae
	 */
Packit b1f7ae
	char *fileroot;
Packit b1f7ae
Packit b1f7ae
	/* The list of files that are encountered while parsing the
Packit b1f7ae
	 * lstfile.
Packit b1f7ae
	 */
Packit b1f7ae
	struct file_list *fl;
Packit b1f7ae
Packit b1f7ae
	/* State of the current assembly file, while parsing the
Packit b1f7ae
	 * lstfile.
Packit b1f7ae
	 */
Packit b1f7ae
	struct state *st_asm;
Packit b1f7ae
Packit b1f7ae
	/* Current line number in the lstfile.  */
Packit b1f7ae
	int lst_curr_line;
Packit b1f7ae
Packit b1f7ae
	/* The list of labels found in the lstfile.  */
Packit b1f7ae
	struct label *l;
Packit b1f7ae
};
Packit b1f7ae
Packit b1f7ae
/* Allocates a new yasm container with @pttfile.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns a non-NULL yasm container object on success; NULL otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern struct yasm *yasm_alloc(const char *pttfile);
Packit b1f7ae
Packit b1f7ae
/* Deallocates and clears all field of @y.
Packit b1f7ae
 * If @y is the NULL pointer, nothing happens.
Packit b1f7ae
 */
Packit b1f7ae
extern void yasm_free(struct yasm *y);
Packit b1f7ae
Packit b1f7ae
/* Assembles the pttfile with yasm and parses all labels.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern int yasm_parse(struct yasm *y);
Packit b1f7ae
Packit b1f7ae
/* Looks up @labelname and stores its address in @addr if found.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern int yasm_lookup_label(const struct yasm *y, uint64_t *addr,
Packit b1f7ae
			     const char *labelname);
Packit b1f7ae
Packit b1f7ae
/* Looks up the special section label "section_@name_@attribute" and stores
Packit b1f7ae
 * its value in @value if found.
Packit b1f7ae
 *
Packit b1f7ae
 * Valid attributes are:
Packit b1f7ae
 *
Packit b1f7ae
 * - start    the section's start address in the binary file
Packit b1f7ae
 * - vstart   the section's virtual load address
Packit b1f7ae
 * - length   the section's size in bytes
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 */
Packit b1f7ae
extern int yasm_lookup_section_label(const struct yasm *y, const char *name,
Packit b1f7ae
				     const char *attribute, uint64_t *value);
Packit b1f7ae
Packit b1f7ae
/* Stores the next pt directive in @pd.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 * Returns -err_internal if @y or @pd is the NULL pointer.
Packit b1f7ae
 * Returns -err_no_directive if there is no pt directive left.
Packit b1f7ae
 */
Packit b1f7ae
extern int yasm_next_pt_directive(struct yasm *y, struct pt_directive *pd);
Packit b1f7ae
Packit b1f7ae
/* Calls pd_parse for the current file and line.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 * Returns -err_no_directive if the current source line contains no PT
Packit b1f7ae
 * directive.
Packit b1f7ae
 */
Packit b1f7ae
extern int yasm_pd_parse(struct yasm *y, struct pt_directive *pd);
Packit b1f7ae
Packit b1f7ae
/* Stores the next line in the asm file into @dest.  The memory behind
Packit b1f7ae
 * @dest must be large enough to store @destlen bytes.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 * Returns -err_internal if @y is the NULL pointer or @dest is NULL, but
Packit b1f7ae
 * @destlen is non-zero.
Packit b1f7ae
 */
Packit b1f7ae
extern int yasm_next_line(struct yasm *y, char *dest, size_t destlen);
Packit b1f7ae
Packit b1f7ae
/* Prints the error message @s together with errstr[@errcode].  File and
Packit b1f7ae
 * line information are printed regarding the current state of @y.
Packit b1f7ae
 *
Packit b1f7ae
 * Returns 0 on success; a negative enum errcode otherwise.
Packit b1f7ae
 * Returns -err_internal if @errcode is not negative.
Packit b1f7ae
 */
Packit b1f7ae
extern int yasm_print_err(const struct yasm *y, const char *s, int errcode);
Packit b1f7ae
Packit b1f7ae
#endif /* YASM_H */