Align AARCH32 version of debug.S with AARCH64

Re-order code (put panic and report_exception at the end of the file).
Export asm_print_* functions.
Add asm_print_line_dec macro, and asm_print_newline func.
Align comments in both AARCH32 and AARCH64 files.
Add blank lines in AARCH64 files to align with AARCH32.

Change-Id: I8e299a27c1390f71f04e260cd4a0e59b2384eb19
Signed-off-by: Yann Gautier <yann.gautier@st.com>
diff --git a/common/aarch32/debug.S b/common/aarch32/debug.S
index f506356..9d410df 100644
--- a/common/aarch32/debug.S
+++ b/common/aarch32/debug.S
@@ -1,71 +1,25 @@
 /*
- * Copyright (c) 2016-2017, ARM Limited and Contributors. All rights reserved.
+ * Copyright (c) 2016-2020, ARM Limited and Contributors. All rights reserved.
  *
  * SPDX-License-Identifier: BSD-3-Clause
  */
 
 #include <arch.h>
 #include <asm_macros.S>
+#include <common/debug.h>
 
+	.globl	asm_print_str
+	.globl	asm_print_hex
+	.globl	asm_print_hex_bits
 	.globl	asm_assert
 	.globl	do_panic
 	.globl	report_exception
 
 /* Since the max decimal input number is 65536 */
 #define MAX_DEC_DIVISOR		10000
-
 /* The offset to add to get ascii for numerals '0 - 9' */
 #define ASCII_OFFSET_NUM	'0'
 
-	.section .rodata.panic_str, "aS"
-panic_msg:
-	.asciz "PANIC at PC : 0x"
-panic_end:
-	.asciz "\r\n"
-
-	/***********************************************************
-	 * The common implementation of do_panic for all BL stages
-	 ***********************************************************/
-func do_panic
-	/* Have LR copy point to PC at the time of panic */
-	sub	r6, lr, #4
-
-	/* Initialize crash console and verify success */
-	bl	plat_crash_console_init
-	cmp	r0, #0
-	beq	1f
-
-	/* Print panic message */
-	ldr	r4, =panic_msg
-	bl	asm_print_str
-
-	/* Print LR in hex */
-	mov	r4, r6
-	bl	asm_print_hex
-
-	/* Print new line */
-	ldr	r4, =panic_end
-	bl	asm_print_str
-
-	bl	plat_crash_console_flush
-
-1:
-	mov	lr, r6
-	b	plat_panic_handler
-endfunc do_panic
-
-	/***********************************************************
-	 * This function is called from the vector table for
-	 * unhandled exceptions. It reads the current mode and
-	 * passes it to platform.
-	 ***********************************************************/
-func report_exception
-	mrs	r0, cpsr
-	and	r0, #MODE32_MASK
-	bl	plat_report_exception
-	no_ret	plat_panic_handler
-endfunc report_exception
-
 #if ENABLE_ASSERTIONS
 .section .rodata.assert_str, "aS"
 assert_msg1:
@@ -79,6 +33,26 @@
 	.asciz " Line 0x"
 #else
 	.asciz " Line "
+
+	/*
+	 * This macro is intended to be used to print the
+	 * line number in decimal. Used by asm_assert macro.
+	 * The max number expected is 65536.
+	 * In: r4 = the decimal to print.
+	 * Clobber: lr, r0, r1, r2, r5, r6
+	 */
+	.macro asm_print_line_dec
+	mov	r6, #10		/* Divide by 10 after every loop iteration */
+	ldr	r5, =MAX_DEC_DIVISOR
+dec_print_loop:
+	udiv	r0, r4, r5			/* Get the quotient */
+	mls	r4, r0, r5, r4			/* Find the remainder */
+	add	r0, r0, #ASCII_OFFSET_NUM	/* Convert to ascii */
+	bl	plat_crash_console_putc
+	udiv	r5, r5, r6			/* Reduce divisor */
+	cmp	r5, #0
+	bne	dec_print_loop
+	.endm
 #endif
 
 /* ---------------------------------------------------------------------------
@@ -100,25 +74,25 @@
 	mov	r5, r0
 	mov	r6, r1
 
-	/* Initialize crash console and verify success */
+	/* Ensure the console is initialized */
 	bl	plat_crash_console_init
+
+	/* Check if the console is initialized */
 	cmp	r0, #0
-	beq	1f
+	beq	_assert_loop
 
-	/* Print file name */
+	/* The console is initialized */
 	ldr	r4, =assert_msg1
 	bl	asm_print_str
 	mov	r4, r5
 	bl	asm_print_str
-
-	/* Print line number string */
 	ldr	r4, =assert_msg2
 	bl	asm_print_str
 
-	/* Test for maximum supported line number */
+	/* Check if line number higher than max permitted */
 	ldr	r4, =~0xffff
 	tst	r6, r4
-	bne	1f
+	bne	_assert_loop
 	mov	r4, r6
 
 #if ARM_ARCH_MAJOR == 7 && !defined(ARMV7_SUPPORTS_VIRTUALIZATION)
@@ -128,22 +102,10 @@
 	 ******************************************************************/
 	bl	asm_print_hex
 #else
-	/* Print line number in decimal */
-	mov	r6, #10			/* Divide by 10 after every loop iteration */
-	ldr	r5, =MAX_DEC_DIVISOR
-dec_print_loop:
-	udiv	r0, r4, r5			/* Quotient */
-	mls	r4, r0, r5, r4			/* Remainder */
-	add	r0, r0, #ASCII_OFFSET_NUM	/* Convert to ASCII */
-	bl	plat_crash_console_putc
-	udiv	r5, r5, r6			/* Reduce divisor */
-	cmp	r5, #0
-	bne	dec_print_loop
+	asm_print_line_dec
 #endif
-
 	bl	plat_crash_console_flush
-
-1:
+_assert_loop:
 #endif /* LOG_LEVEL >= LOG_LEVEL_INFO */
 	no_ret	plat_panic_handler
 endfunc asm_assert
@@ -171,8 +133,11 @@
  * Clobber: lr, r0 - r3, r5
  */
 func asm_print_hex
-	mov	r3, lr
 	mov	r5, #32  /* No of bits to convert to ascii */
+
+	/* Convert to ascii number of bits in r5 */
+asm_print_hex_bits:
+	mov	r3, lr
 1:
 	sub	r5, r5, #4
 	lsr	r0, r4, r5
@@ -190,3 +155,53 @@
 	bne	1b
 	bx	r3
 endfunc asm_print_hex
+
+	/***********************************************************
+	 * The common implementation of do_panic for all BL stages
+	 ***********************************************************/
+
+.section .rodata.panic_str, "aS"
+	panic_msg: .asciz "PANIC at PC : 0x"
+	panic_end: .asciz "\r\n"
+
+func do_panic
+	/* Have LR copy point to PC at the time of panic */
+	sub	r6, lr, #4
+
+	/* Initialize crash console and verify success */
+	bl	plat_crash_console_init
+
+	/* Check if the console is initialized */
+	cmp	r0, #0
+	beq	_panic_handler
+
+	/* The console is initialized */
+	ldr	r4, =panic_msg
+	bl	asm_print_str
+
+	/* Print LR in hex */
+	mov	r4, r6
+	bl	asm_print_hex
+
+	/* Print new line */
+	ldr	r4, =panic_end
+	bl	asm_print_str
+
+	bl	plat_crash_console_flush
+
+_panic_handler:
+	mov	lr, r6
+	b	plat_panic_handler
+endfunc do_panic
+
+	/***********************************************************
+	 * This function is called from the vector table for
+	 * unhandled exceptions. It reads the current mode and
+	 * passes it to platform.
+	 ***********************************************************/
+func report_exception
+	mrs	r0, cpsr
+	and	r0, #MODE32_MASK
+	bl	plat_report_exception
+	no_ret	plat_panic_handler
+endfunc report_exception