Skip to content

Commit 1550f2d

Browse files
authored
Merge pull request #3405 from danpoe/docs/remove-asm
Document remove asm
2 parents 2d5d8f9 + 6e05d99 commit 1550f2d

File tree

2 files changed

+89
-12
lines changed

2 files changed

+89
-12
lines changed

src/goto-programs/remove_asm.cpp

+49-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************\
22
3-
Module: Remove 'asm' statements by compiling into suitable standard
4-
code
3+
Module: Remove 'asm' statements by compiling them into suitable
4+
standard goto program instructions
55
66
Author: Daniel Kroening
77
@@ -10,7 +10,8 @@ Date: December 2014
1010
\*******************************************************************/
1111

1212
/// \file
13-
/// Remove 'asm' statements by compiling into suitable standard code
13+
/// Remove 'asm' statements by compiling them into suitable standard goto
14+
/// program instructions
1415

1516
#include "remove_asm.h"
1617

@@ -61,6 +62,13 @@ class remove_asmt
6162
goto_programt &dest);
6263
};
6364

65+
/// Adds a call to a library function that implements the given gcc-style inline
66+
/// assembly statement
67+
///
68+
/// \param function_base_name: Name of the function to call
69+
/// \param code: gcc-style inline assembly statement to translate to function
70+
/// call
71+
/// \param dest: Goto program to append the function call to
6472
void remove_asmt::gcc_asm_function_call(
6573
const irep_idt &function_base_name,
6674
const code_asmt &code,
@@ -128,6 +136,13 @@ void remove_asmt::gcc_asm_function_call(
128136
}
129137
}
130138

139+
/// Adds a call to a library function that implements the given msc-style inline
140+
/// assembly statement
141+
///
142+
/// \param function_base_name: Name of the function to call
143+
/// \param code: msc-style inline assembly statement to translate to function
144+
/// call
145+
/// \param dest: Goto program to append the function call to
131146
void remove_asmt::msc_asm_function_call(
132147
const irep_idt &function_base_name,
133148
const code_asmt &code,
@@ -171,7 +186,12 @@ void remove_asmt::msc_asm_function_call(
171186
}
172187
}
173188

174-
/// removes assembler
189+
/// Translates the given inline assembly code (which must be in either gcc or
190+
/// msc style) to non-assembly goto program instructions
191+
///
192+
/// \param instruction: The goto program instruction containing the inline
193+
/// assembly statements
194+
/// \param dest: The goto program to append the new instructions to
175195
void remove_asmt::process_instruction(
176196
goto_programt::instructiont &instruction,
177197
goto_programt &dest)
@@ -188,7 +208,11 @@ void remove_asmt::process_instruction(
188208
DATA_INVARIANT(false, "unexpected assembler flavor");
189209
}
190210

191-
/// removes gcc assembler
211+
/// Translates the given inline assembly code (in gcc style) to non-assembly
212+
/// goto program instructions
213+
///
214+
/// \param code: The inline assembly code statement to translate
215+
/// \param dest: The goto program to append the new instructions to
192216
void remove_asmt::process_instruction_gcc(
193217
const code_asmt &code,
194218
goto_programt &dest)
@@ -349,7 +373,11 @@ void remove_asmt::process_instruction_gcc(
349373
dest.destructive_append(tmp_dest);
350374
}
351375

352-
/// removes msc assembler
376+
/// Translates the given inline assembly code (in msc style) to non-assembly
377+
/// goto program instructions
378+
///
379+
/// \param code: The inline assembly code statement to translate
380+
/// \param dest: The goto program to append the new instructions to
353381
void remove_asmt::process_instruction_msc(
354382
const code_asmt &code,
355383
goto_programt &dest)
@@ -449,7 +477,10 @@ void remove_asmt::process_instruction_msc(
449477
dest.destructive_append(tmp_dest);
450478
}
451479

452-
/// removes assembler
480+
/// Replaces inline assembly instructions in the goto function by non-assembly
481+
/// goto program instructions
482+
///
483+
/// \param goto_function: The goto function
453484
void remove_asmt::process_function(
454485
goto_functionst::goto_functiont &goto_function)
455486
{
@@ -478,14 +509,23 @@ void remove_asmt::process_function(
478509
remove_skip(goto_function.body);
479510
}
480511

481-
/// removes assembler
512+
/// \copybrief remove_asm(goto_modelt &)
513+
///
514+
/// \param goto_functions: The goto functions
515+
/// \param symbol_table: The symbol table
482516
void remove_asm(goto_functionst &goto_functions, symbol_tablet &symbol_table)
483517
{
484518
remove_asmt rem(symbol_table, goto_functions);
485519
rem();
486520
}
487521

488-
/// removes assembler
522+
/// Replaces inline assembly instructions in the goto program (i.e.,
523+
/// instructions of kind `OTHER` with a `code` member of type `code_asmt`) with
524+
/// an appropriate (sequence of) non-assembly goto program instruction(s). At
525+
/// present only a small number of x86 and Power instructions are supported.
526+
/// Unrecognised assembly instructions are ignored.
527+
///
528+
/// \param goto_model: The goto model
489529
void remove_asm(goto_modelt &goto_model)
490530
{
491531
remove_asm(goto_model.goto_functions, goto_model.symbol_table);

src/goto-programs/remove_asm.h

+40-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*******************************************************************\
22
3-
Module: Remove 'asm' statements by compiling into suitable standard
4-
code
3+
Module: Remove 'asm' statements by compiling them into suitable
4+
standard goto program instructions
55
66
Author: Daniel Kroening
77
@@ -10,7 +10,44 @@ Date: December 2014
1010
\*******************************************************************/
1111

1212
/// \file
13-
/// Remove 'asm' statements by compiling into suitable standard code
13+
/// Remove 'asm' statements by compiling them into suitable standard goto
14+
/// program instructions
15+
16+
/// Inline assembly statements in the source program (in either gcc- or
17+
/// msc-style) are represented by instructions of kind `OTHER` with a `code`
18+
/// member of type `code_asmt` in the goto program.
19+
///
20+
/// For example, the gcc inline assembly statement below
21+
///
22+
/// ```
23+
/// int input = 1;
24+
/// int result;
25+
///
26+
/// asm volatile (
27+
/// "mov %1, %0; add $1, %0"
28+
/// : "=r" (result)
29+
/// : "r" (input)
30+
/// : "cc");
31+
/// ```
32+
///
33+
/// would be represented by a `code_asmt` statement with `op0()` being the
34+
/// string literal "mov %1, %0; add $1, %0" (as a `string_constantt` expression)
35+
/// and `op1()`, `op2()`, and `op3()`, respectively, containing the output list,
36+
/// input list, and clobbered registers.
37+
///
38+
/// The `remove_asm()` function replaces the inline assembly statements in a
39+
/// given goto program with appropriate (sequences of) non-assembly goto program
40+
/// instructions.
41+
///
42+
/// It first parses the assembly instructions string (via `assembler_parsert`)
43+
/// and inspects the input/output/clobber lists. Then, it generates a sequence
44+
/// of goto program instructions that either directly implement the assembly
45+
/// instructions, or call a suitable library function (see
46+
/// `library/x86_assembler.c`).
47+
///
48+
/// At present only a small number of x86 and Power instructions are supported.
49+
/// Unrecognised assembly instructions are ignored (i.e., they are simply
50+
/// removed from the goto program).
1451

1552
#ifndef CPROVER_GOTO_PROGRAMS_REMOVE_ASM_H
1653
#define CPROVER_GOTO_PROGRAMS_REMOVE_ASM_H

0 commit comments

Comments
 (0)